Find rows in matrix based on columns value

36 ビュー (過去 30 日間)
Stewart Tan
Stewart Tan 2019 年 9 月 2 日
コメント済み: Andrei Bobrov 2019 年 9 月 2 日
If i have a matrix:
mat = [1 2 3 4;
5 6 7 8;
9 10 11 12];
How do i find the row index where the column has value 10 and 11 for example? In this case, the row index will be 3 since it has columns with 10 and 11.
I tried doing something like:
find(mat(:,2) == 10 && mat(:,3) == 11)
But it doesn't work.
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 9 月 2 日
find(mat(:,2) == 10 & mat(:,3) == 11)

サインインしてコメントする。

採用された回答

Robert U
Robert U 2019 年 9 月 2 日
編集済み: Robert U 2019 年 9 月 2 日
Hi Steward Tan,
in the given matrix there is no single column that contains the values 10 AND 11. If you want to find the rows that contain 10 AND 11 you could use:
tmp = arrayfun(@(dIn) find(any(mat(dIn,:) == 10) & any(mat(dIn,:) == 11)),1:size(mat,1),'UniformOutput',false);
tmp(cellfun(@isempty,tmp)) = {0};
row = find(cell2mat(tmp));
If you want to find the values 10 OR 11 within the matrix, and return the rows they have been found in, one way might be:
mat = [1 2 3 4;
5 6 7 8;
9 10 11 12];
[row,~] = find(mat == 10 | mat == 11);
row = unique(row);
Kind regards,
Robert
  2 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 2 日
? There are only three rows whereas this answer gives 6 & 9 ?
>> mat = [1 2 3 4;
5 6 7 8;
9 10 11 12];
[row,~] = unique(find(mat == 10 | mat == 11))
row =
6
9
>>
Robert U
Robert U 2019 年 9 月 2 日
編集済み: Robert U 2019 年 9 月 2 日
My bad, the unique command needs to be written in an extra line to capture rows from find. I edited my answer.

サインインしてコメントする。

その他の回答 (2 件)

madhan ravi
madhan ravi 2019 年 9 月 2 日
編集済み: madhan ravi 2019 年 9 月 2 日
ix = sum(ismember(mat,[10,11]),2)==2;
row_index = find(ix)
edit:
row_index = find(sum(~mod(mod(mat,10),11),2)==1)
  3 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 2 日
編集済み: madhan ravi 2019 年 9 月 2 日
Yes Andrei, I realised just before your comment :). Hi Andrei, how about:
m=any(~mod(mat,10),2) & any(~mod(mat,11),2);
w=find(m)
Andrei Bobrov
Andrei Bobrov 2019 年 9 月 2 日
All right! +1.

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2019 年 9 月 2 日
編集済み: Andrei Bobrov 2019 年 9 月 2 日
My case for mat:
mat = [1 11 3 10
5 6 10 10
9 10 11 12];
mat2 = sort(mat,2);
[m,n] = size(mat);
mat3 = mat2([(1:end-m)',(m+1:end)']);
iii = mod((1:m*n-m)'-1,m)+1;
out = sort(iii(ismember(mat3,[10,11],'rows')));
or
[i1,~] = find(mat == 10);
[i2,~] = find(mat == 11);
out = intersect(i1,i2);
another variant:
out = all(any(mat == reshape([10,11],1,1,[]),2),3);
  6 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 9 月 2 日
I'm sorry Robert, my mistake!
Stewart Tan
Stewart Tan 2019 年 9 月 2 日
Thank you all for the various answers for this question of mine. Wish there was a way to accept more than one answers as they had been of good help. Voted up! Cheers!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by