How to combine two vectors column-by-column?
23 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have two matrices, A and B.
A = [1,3,5,7,9,11,13,15]
B = [2,4,6,8,10,12,14,16]
How can I combine these two to give me a third matrix C, which is as follows?
C = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
So, basically, C should be [A(:,1), B(:,1), A(:,2), B(:,2), ...]
0 Commenti
Risposta accettata
Stephen23
il 17 Mar 2023
A = [1,3,5,7;9,11,13,15;17,19,21,23]
B = [2,4,6,8;10,12,14,16;18,20,22,24]
C = reshape(permute(cat(3,A,B),[1,3,2]),size(A,1),[])
1 Commento
Stephen23
il 17 Mar 2023
A = [1,3,5,7;9,11,13,15;17,19,21,23];
B = [2,4,6,8;10,12,14,16;18,20,22,24];
Another approach:
C = repelem(A,1,2);
C(:,2:2:end) = B
Più risposte (1)
Sarvesh Kale
il 14 Mar 2023
Hi Jake,
You can see the following code snippet
A = [1,3,5,7,9,11,13,15];
B = [2,4,6,8,10,12,14,16];
C=[A;B] % row wise concatenation
C=C(:)'; % traversal will be column wise as matrix elements are stored in column major format
C
I hope this helps your query
Thank you
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!