How to I combine different numbers of columns from two matrices?
显示 更早的评论
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.
采纳的回答
更多回答(1 个)
Guillaume
2015-10-26
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!