Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
return indices

Subject: return indices

From: ravi

Date: 10 Oct, 2010 18:39:04

Message: 1 of 8

Hi,

I have a matrix :

F =
                    0.6892 134.5343 25.5343
                    23.108 130 11
                     3.31 108 25
                    55.45 77 32
                  136.7508 6 2

I am given label of the F matrix for each of the 5 values in each of the 3 columns as follows:

I =
                                     2 1 1
                                     3 3 2
                                     4 4 4
                                     5 5 5
                                     6 6 6

For each column and its corresponding row value how can I get the row number and column number index for values <4 ?

The answer I am working towards should be:

[1 4;
 3 6]

where 1 , 3 are representative of the indices of the identified columns with the value<4 and 4 , 6 are the corresponding indices.

Please help me out guys.

thanks

Subject: return indices

From: Roger Stafford

Date: 10 Oct, 2010 19:45:05

Message: 2 of 8

  You seem to have omitted the value 0.6892 which is also less than 4. Assuming you want that included, do this:

 [r,c] = find(F < 4);
 R = [c,I(sub2ind(size(I),r,c))];

Roger Stafford

Subject: return indices

From: ravi

Date: 10 Oct, 2010 19:56:03

Message: 3 of 8

thank you roger

Subject: return indices

From: Image Analyst

Date: 10 Oct, 2010 19:57:03

Message: 4 of 8

Well I got this far:
F = [0.6892 134.5343 25.5343;...
  23.108 130 11;...
 3.31 108 25;...
  55.45 77 32;...
136.7508 6 2]
I =[2 1 1;...
    3 3 2;...
    4 4 4;...
    5 5 5;...
    6 6 6]
% Find values in F less than 4
[rows cols] = find(F < 4)
% Convert from row, col coordinates into linear indices.
linearIndices = sub2ind(size(F), rows, cols)
% Just to check, let's see what we found...
extractedFValues = F(linearIndices) % Just to verify...
% Get the indices from the I matrix
extractedIValues = I(linearIndices)

rows =
     1
     3
     5
cols =
     1
     1
     3
linearIndices =
     1
     3
    15
extractedFValues =
    0.6892
    3.3100
    2.0000
extractedIValues =
     2
     4
     6

but your description fails in general because some columns may have different number of numbers that are less than 4, so your "result" array might have 2 numbers in one row, but 3 in another, and 6 in another, for the general case of larger matrices and values that can be anything. Also, I'm not sure why you found 2 values less than 4 and I found 3 but I'm not sure how to put them into your results output array unless I switch to a cell array. I'd put 2 into one row and 1 into the other row.

Subject: return indices

From: Image Analyst

Date: 10 Oct, 2010 20:02:04

Message: 5 of 8

Looks like Roger found the same inconsistency as me, and solved it by putting the additional stuff on an additional row. I suggest you go with his code - it is essentially what I was going after (until I hit the inconsistency), mine is just a bit more verbose while his is more compact.

Subject: return indices _ issue with find function

From: ravi

Date: 11 Oct, 2010 00:11:03

Message: 6 of 8

Roger,

The find function works well and outputs 'r' and 'c' in [r,c] as a single column when the F matrix is large as the original F matrix.

So in the example I posted,

>> [r,c] = find(F < 4)

r =

     1
     3
     5


c =

     1
     1
     3

However , when the F matrix is small, specifically in the form of the below example, 'r' and 'c' outputs into


>>F = [1 0.2]

     I= [4 5]

>>[r,c] = find(F < 4)

r =

     1 1


c =

     1 2


This directly messes up my output when I use the code you helped me out with,i.e.
R = [c,I(sub2ind(size(I),r,c))];

It wrongly outputs as:

R =

    1 2 4 5

Instead it should be

R = [ 1 4;
        2 5]

I've been trying to sort it out for a while and cant seem how to fix this.My code is an iterative one , where the F matrix gets smaller , this is how I recognised this problem.

Need some help please.

Thanks again
ravi

Subject: return indices

From: ravi

Date: 11 Oct, 2010 00:34:05

Message: 7 of 8

Roger,

The find function works well and outputs 'r' and 'c' in [r,c] as a single column when the F matrix is large as the original F matrix.

So in the example I posted,

>> [r,c] = find(F < 4)

r =

     1
     3
     5


c =

     1
     1
     3

However , when the F matrix is small, specifically in the form of the below example, 'r' and 'c' outputs into


>>F = [1 0.2]

     I= [4 5]

>>[r,c] = find(F < 4)

r =

     1 1


c =

     1 2


This directly messes up my output when I use the code you helped me out with,i.e.
R = [c,I(sub2ind(size(I),r,c))];

It wrongly outputs as:

R =

    1 2 4 5

Instead it should be

R = [ 1 4;
        2 5]

I've been trying to sort it out for a while and cant seem how to fix this.My code is an iterative one , where the F matrix gets smaller , this is how I recognised this problem.

Need some help please.

Thanks again
ravi

Subject: return indices _ issue with find function

From: Roger Stafford

Date: 11 Oct, 2010 01:14:04

Message: 8 of 8

"ravi " <ravi_071@hotmail.com> wrote in message <i8tkmn$9m4$1@fred.mathworks.com>...
> .......
> It wrongly outputs as:
>
> R =
> 1 2 4 5
>
> Instead it should be
>
> R = [ 1 4;
> 2 5]
> .......
- - - - - - - - - -
  Yes, if you are going to use vectors rather than two-dimensional arrays for F and I, you will have to force those two quantities to be column vectors:

 [r,c] = find(F < 4);
 t = I(sub2ind(size(I),r,c));
 R = [c(:),t(:)];

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us