inserting vectors into another new vector
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohanned Al Gharawi
le 6 Déc 2019
Réponse apportée : Mohanned Al Gharawi
le 7 Déc 2019
Hello everybod,
Let’s say I have two vectors, A=[7 17 27] and B=[13 22], and later produce a new vector C as shown in the below process:
put the first number from A as the first number in C and put the first number form B as the second number in C and then put the second number from A as the third number in C then put the second number from B as the fourth number in C and keep going on the same process to the end.
That means, I should have the final result for C as [7 13 17 22 27].
Thank you
2 commentaires
the cyclist
le 6 Déc 2019
Will B always be exactly one element shorter than A? If not, can you please tell us more detail? For example, could A be the shorter one? Do we always use all elements from both vectors?
Réponse acceptée
JESUS DAVID ARIZA ROYETH
le 6 Déc 2019
Modifié(e) : JESUS DAVID ARIZA ROYETH
le 6 Déc 2019
A=[7 17 27 35];
B=[13 22 30 15];
N = max(numel(A),numel(B));
C=[A nan(1,N-numel(A)); B nan(1,N-numel(B))];
C(isnan(C))=[]
C=C(:)'
2 commentaires
JESUS DAVID ARIZA ROYETH
le 6 Déc 2019
I edited my answer, try again, did the test with several cases and it works fine
Plus de réponses (3)
the cyclist
le 6 Déc 2019
Here is another way:
A = [7 17 27 35];
B = [13 22 30 31];
C = [A; [B nan(numel(A)-numel(B),1)]];
C = C(1:(numel(A)+numel(B)));
0 commentaires
Voir également
Catégories
En savoir plus sur Gamma Functions 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!