フィルターのクリア

find row with certain values

267 ビュー (過去 30 日間)
Daniel
Daniel 2011 年 12 月 20 日
コメント済み: Soyy Tuffjefe 2019 年 8 月 27 日
Hello I am looking for a (simple) way to get the index of a row in which two (or n) values exist
example: looking for 4 and 5 in
[1 5 6; 5 4 3; 9 4 2]
will give me 2, because only row 2 has both 4 and 5 in it
Thanks
Daniel
  2 件のコメント
Daniel
Daniel 2011 年 12 月 22 日
thank you all for the answers
since all of your answers do what I wanted I'll choose the "winner" by:
1. short code
2. running time
3. running time on large matrices and/or many numbers to find
Naz
Naz 2011 年 12 月 22 日
I deleted my answer so it will be easier for you to make a decision.

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

採用された回答

Robert Cumming
Robert Cumming 2011 年 12 月 21 日
similar to the intersect answer - but I recoded intersect as its quite slow:
x=[1 2 3;4 5 6;3 2 1];
[a b]=find(x==4);
[c d]=find(x==5);
index = c.*NaN;
for kk=1:length(c)
check = a(a==c(kk))';
if ~isempty ( check )
index(kk) = check;
end
end
output = index(~isnan(index));
  2 件のコメント
kd p
kd p 2017 年 12 月 6 日
output doesn't show for this!
the cyclist
the cyclist 2017 年 12 月 6 日
編集済み: the cyclist 2017 年 12 月 6 日
What do you mean by "doesn't show"?
That code calculates the output (at least for me). Nothing is displayed to the screen because the line ends with a semicolon, which suppresses this display.
You can removed that semicolon, or just type
output
to see the result.
If that code is not even calculating the output for you, then please post the full error message you are getting.

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

その他の回答 (5 件)

Jan
Jan 2011 年 12 月 20 日
X = [1 5 6; 5 4 3; 9 4 2]
index = and(any(X == 4, 2), any(X == 5, 2));
[EDITED: Apply ANY along 2nd dim, thanks Cyclist!]
  1 件のコメント
the cyclist
the cyclist 2011 年 12 月 20 日
I think the "any" here should be over dimension 2, not dimension 1.

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


Malcolm Lidierth
Malcolm Lidierth 2011 年 12 月 20 日
>> x=[1 5 6; 5 4 3; 9 4 2];
>> [a b]=find(x==4);
>> [c d]=find(x==5);
>> intersect(a,c)
ans =
2

the cyclist
the cyclist 2011 年 12 月 20 日
Trust but verify this code:
x = [1 5 6; 5 4 3; 9 4 2]
want(1,1,:) = [4 5];
indexToDesiredRows = all(any(bsxfun(@eq,x,want),2),3)
rowNumbers = find(indexToDesiredRows)

Sean de Wolski
Sean de Wolski 2011 年 12 月 20 日
How about ismember with a for-loop?
doc ismember
Example
A = [1 5 6; 5 4 3; 9 4 2];
want = [4 5];
szA = size(A,1);
idx = false(szA,1);
for ii = 1:szA
idx(ii) = all(ismember(want,A(ii,:)));
end
idx will be a logical vector of rows with 4 and 5. If you want the numeric values:
find(idx)
This will be the most scalable method if say you want 10 different numbers to be present in each row. Calling any/ intersect / all that many times and/or using that many dimensions is not really feasible.
  1 件のコメント
Soyy Tuffjefe
Soyy Tuffjefe 2019 年 8 月 27 日
Suppose that A = [1 5 6 13 22; 9 5 4 6 37; 7 1 4 22 37];
and want = [5 6; 1 22; 4,37];
Please, Can you modify your code for find two o more rows; or any idea for me to try this job with your code I have a want matriz of 100x4 and A matrix of 1000x5 order.
Thanks

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


Ankit
Ankit 2013 年 4 月 20 日
>> x = [1 2 3 4 1 2 2 1]; find(sum(bsxfun(@eq,x',[1:3]),2)==1) ans =
1
2
3
5
6
7
8
Similar things can be done for an array rather than just a vector (x above).

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by