Hi I have a 10 x 2 logical matrix and I would need to find out the location of the rows which both columns are ones i.e 1 1.
May I know how I may write for this? Thank you!

 Réponse acceptée

madhan ravi
madhan ravi le 22 Août 2019
Modifié(e) : madhan ravi le 22 Août 2019

0 votes

Since you have a logical matrix it’simply:
ix=all(matrix ,2); % logical indexing is efficient
Wanted=find(ix)

6 commentaires

Cside
Cside le 22 Août 2019
oops that only gave me a list of numbers 1 - 10, without the criteria of both columns being ones
matrix(ix,:)
however we can already predict that the result would just be a variable number of rows with both columns being 1, so this is not of interest. What is of interest is either the logical row mask, ix, or else the numeric row indices, which are in Wanted.
madhan's code is correct.
Cside
Cside le 22 Août 2019
I would like to find the position of the rows that fit the criteria, and have it presented in a vector. perhaps I was slightly vague in my question, still learning! :)
madhan ravi
madhan ravi le 22 Août 2019
Modifié(e) : madhan ravi le 22 Août 2019
Thank you , sir Walter.
>> matrix
matrix =
4×2 logical array
1 1
0 1
1 0
0 1
>> ix=all(matrix ,2)
ix =
4×1 logical array
1
0
0
0
>> Wanted = find(ix) % this is what you want
Wanted =
1
>> matrix(ix,:)
ans =
1×2 logical array
1 1
>>
Cside
Cside le 22 Août 2019
thank you madhan for being so clear with your answer :)
madhan ravi
madhan ravi le 22 Août 2019
While learning always visualize with a simple and short example to understand what's going on.

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 22 Août 2019

0 votes

Read about ismember
idx = round(rand(10,2)) ;
vec = [1 1] ;
[c,ia] = ismember(idx,vec,'rows') ;
iwant = find(c) ;
idx(c,:)
idx(iwant,:)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by