search for rows of a matrix within bigger matrix (without for loop)

A=[1 6 ;5 4 ;1 5 ;6 9 ;2 1;9 4;7 7]; B=[1 6 ;6 9 ;7 7];
I would like to search for rows of a matrix B within A. the result gives me the indices of matched rows [1 4 7] It works with me with for loop.However, i am trying to find another way to do it without for loop,any ideas please?
Thank you in advance

 採用された回答

Honglei Chen
Honglei Chen 2012 年 3 月 26 日

1 投票

[C,IA] = intersect(A,B,'rows')
or
[tf,rowA] = ismember(B,A,'rows')
see
doc intersect
doc ismember

4 件のコメント

Saleem Infinity
Saleem Infinity 2012 年 3 月 26 日
Thank you Honglei Chen,
i have checked it out, but apparently it is not enough,
since duplicate rows in B will not be recognized.
if B=[1 6 ;1 6;6 9 ;7 7]; the result will be also [1 4 7] and i want it to be like [1 1 4 7]
Oleg Komarov
Oleg Komarov 2012 年 3 月 26 日
[tf,rowA] = ismember(B,A,'rows')
You need rowA
Saleem Infinity
Saleem Infinity 2012 年 3 月 26 日
Thank you SO MUCH Oleg, that is exactly what i wanted :)
i really appreciate your help.
Thank you again
Honglei Chen
Honglei Chen 2012 年 3 月 26 日
I added Oleg's comment to the answer so it is more visible to other people.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by