Output of the row of a vector which has the same entry as a second vector
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
Problem: Two vectors a and b. Id like to know in which row of the vector a, the entries of a and b are the same. Output vector r. if a entry in b occurs twice/triple/... the output of the row should also be twice/triple/...
I tried it like this:
a=[1 3 5 8 9 10 13 16]';
b=[2 5 1 1 8 8 8]';
r=find(ismember(a,b))
Solution: r =[1;3;4]
desired solution: r =[1;1;3;4;4;4]
Thanks in advance for your help!
1 commentaire
Andrei Bobrov
le 1 Déc 2012
Modifié(e) : Andrei Bobrov
le 1 Déc 2012
[c,d] = ismember(sort(b),a)
r = d(c);
Réponse acceptée
Plus de réponses (1)
Azzi Abdelmalek
le 1 Déc 2012
Modifié(e) : Azzi Abdelmalek
le 1 Déc 2012
a=[1 3 5 8 9 10 13 16]';
b=[2 5 1 1 8 8 8]';
un=ones(numel(b),1)
out=cell2mat(arrayfun(@(x) un(b==a(x))*x,(1:numel(a))','un',0))
0 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!