Find indices of one array in another array.

I have two different sized two column arrays, like the example below:
A = [1 1; 1 2; 2 2; 2 3; 3 3; 3 4; 4 3; 4 4];
B = [1 1; 2 2; 3 3; 4 4];
Now I want to find the indices of the rows in A that are equal to B. So the answer should be:
idx_rows = [1 3 5 8]
How can I do this?
Thanks!

 採用された回答

Birdman
Birdman 2018 年 3 月 28 日
編集済み: Birdman 2018 年 3 月 28 日

0 投票

[~,idx]=setdiff(A,B,'rows');
idx_rows=setdiff(1:size(A,1),idx)

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 3 月 28 日
編集済み: Stephen23 2018 年 3 月 28 日

2 投票

The simplest solution is to use ismember:
>> A = [1 1; 1 2; 2 2; 2 3; 3 3; 3 4; 4 3; 4 4];
>> B = [1 1; 2 2; 3 3; 4 4];
>> [~,idx] = ismember(B,A,'rows')
idx =
1
3
5
8

カテゴリ

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

質問済み:

2018 年 3 月 28 日

編集済み:

2018 年 3 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by