Find different arrays in a matrix

2 vues (au cours des 30 derniers jours)
EldaEbrithil
EldaEbrithil le 20 Août 2020
Modifié(e) : EldaEbrithil le 20 Août 2020
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 commentaire
EldaEbrithil
EldaEbrithil le 20 Août 2020
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

Connectez-vous pour commenter.

Réponse acceptée

David Hill
David Hill le 20 Août 2020
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 commentaire
EldaEbrithil
EldaEbrithil le 20 Août 2020
Modifié(e) : EldaEbrithil le 20 Août 2020
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by