How do I merge vectors?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Maroulator
le 29 Déc 2015
Réponse apportée : Star Strider
le 29 Déc 2015
I have a 10X1 vector A=[1;3;7;10;12;14;15;18;20;21] and a 10X1 vector B=[3;12;15;18;20;0;0;0;0;0]. Is there a way to come up with a 10X2 vector C such that the equal elements in each vector sit at the same row in vector C? I want to retain all elements of vector A in vector C and keep only the elements from vector B that have values equal to the one elements in vector A. Ideally, My final vector C would look something like this: C=[1 0;3 3;7 0;10 0;12 12;14 0;15 15;18 18;20 20;21 0]. Any thoughts?
0 commentaires
Réponse acceptée
Star Strider
le 29 Déc 2015
The intersect function works best here:
A=[1;3;7;10;12;14;15;18;20;21];
B=[3;12;15;18;20;0;0;0;0;0];
[m,ia,ib] = intersect(A, B, 'stable');
C = [A zeros(size(A,1),1)];
C(ia,2) = B(ib)
C =
1 0
3 3
7 0
10 0
12 12
14 0
15 15
18 18
20 20
21 0
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!