user1166251
user1166251

Reputation: 115

Find a specific value in a specific row

I have a matrix A(97x13) ranging from 0 to 90

I would like to find in specific rows which values are lower than 45.

find(A(:,[4,7,10])<45);

My problem is that I don't know how to relate the resulting indices with the entire A matrix.

Can someone help me with that?

Upvotes: 0

Views: 56

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

The result is in the form of a linear index. You can convert to row and column using ind2sub:

cols = [4,7,10];
ind = find(A(:,cols)<45);
[r, c] = ind2sub([size(A,1) numel(rows)], ind);
c = cols(c); %// change from "1,2,3" to "4,7,10"

To save some running time, you can use a "manual" version of ind2sub:

cols = [4,7,10];
ind = find(A(:,cols)<45);
r = mod(ind-1,size(A,1))+1;
c = cols(ceil(ind/size(A,1)));

Example:

A =
     8    66    87    57     4    24    10    82     3    55
    24    44    50    62    80    31    59    81    67    56
    73    53    47    36    83    62    45    31    46    78
     3    22    21    34    72    13    71    63    44    73
    84    42    45    89     9    65    65    18    82    52

gives

r =
     3
     4
     1

c =
     4     4     7

Upvotes: 2

Marcin
Marcin

Reputation: 238081

Use ind2sub to get rows and columns, for example:

>> A=round(rand(5,3)*90)

A =

    85    19    18
    79    27    20
    50    42    15
    56    21    20
    53    76    39

>> [I,J] = ind2sub(size(A), find(A(:,[1,3])<45))

I =

     1
     2
     3
     4
     5


J =

     2
     2
     2
     2
     2

>> 

Upvotes: 1

Related Questions