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 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