Find different arrays in a matrix

2 ビュー (過去 30 日間)
EldaEbrithil
EldaEbrithil 2020 年 8 月 20 日
編集済み: EldaEbrithil 2020 年 8 月 20 日
Hi all
i have a matrix like that A=[1 2 3; 4 5 6; 7 8 9] i want the index of the rows that contain B=[1 3;4 5;7 9;4 6;2 3] so in this case the rows are:1, 2, 3, 2, 1
Thank you for the help
Regards
  1 件のコメント
EldaEbrithil
EldaEbrithil 2020 年 8 月 20 日
I have tried
[r,c]=ismember(A,B,'rows')
but it gives me error due to the fact that B and A must have the same dimension

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

採用された回答

David Hill
David Hill 2020 年 8 月 20 日
A=[1 2 3; 4 5 6; 7 8 9];
B=[1 3;4 5;7 9;4 6;2 3];
a=zeros(size(B,1),1);
for k=1:size(B,1)
a(k)=find(sum(ismember(A,B(k,:)),2));
end
  1 件のコメント
EldaEbrithil
EldaEbrithil 2020 年 8 月 20 日
編集済み: EldaEbrithil 2020 年 8 月 20 日
Hi David thank you for answer me
i also found a solition of this type:
A = [1 14 24;2 15 25;1 16 72;1 14 24] % A sample matrix for demonstration...
c=[14 24;15 25;14 24]
[rig,col]=size(c);
for i=1:rig
I(:,i) = double(ismember(A(:,2:3),c(i,:),'rows'));
end
[rig,colo]=ind2sub(size(I),find(I>0))
the only inconvenient is that, due to the fact that A contain repeated lines, I is a unitary matrix that contains multiple (in this case two) ones in the same column( in this case in the first and last columns)
1 0 1
0 1 0
0 0 0
1 0 1
i don't want that, i want a single one for each column: i could i remove the multiple one in the columns? Whether it is the first one in the first or last row makes no difference, the important thing is that there is only one per column

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by