How to I combine different numbers of columns from two matrices?
Afficher commentaires plus anciens
I have two matrices, A and B, and would like to generate a third matrix, C, that contains column 1 of A followed by columns 1:3 of B, then column 2 of A followed by columns 4:6 of B, and so on, until the end of matrix A is reached. I know I can individually generate each group of [A(:,x),B(:,x:x+2)] and then concatenate them, but I'd like to make one loop function to read matrices of any size.
Réponse acceptée
Plus de réponses (1)
Guillaume
le 26 Oct 2015
I would have done it with a logical array:
A=repmat(1:4,3,1); % A has its column filled with number 1 to 4
B=repmat(5:16,3,1); % B has its column filled with number 5 to 16
filler = repmat(logical([1 0 0 0]), size(A)); %logical array. 1 indicates use A, 0 indicates use B.
C = zeros(size(filler)); %output array
C(filler) = A; %fill 1 with A
C(~filler) = B %fill 0 with B
Simpler than calculating column indices.
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!