Find different arrays in a matrix

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Creating 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