Reputation: 1
I want to combine columns of matrices, for example,
A=[1,2,3;4,5,6]';B=[1,3,5;2,9,0]';
and I want
C1=[1,2,3;1,3,5]'
C2=[1,2,3;2,9,0]'
C3=[4,5,6;1,3,5]'
C4=[4,5,6;2,9,0]'
How do I do that in matlab? Is there a function that does this?
Thanks!
Upvotes: 0
Views: 53
Reputation: 112659
Is this what you want?
[ii, jj] = ndgrid(1:size(A,2));
C = permute(cat(3, A(:,jj), B(:,ii)), [1 3 2]);
The result is a 3D array such that (C(:,:,1)
is your C1
, etc:
C(:,:,1) =
1 1
2 3
3 5
C(:,:,2) =
1 2
2 9
3 0
C(:,:,3) =
4 1
5 3
6 5
C(:,:,4) =
4 2
5 9
6 0
Upvotes: 0
Reputation: 533
This should do the trick:
A=[1,2,3;4,5,6]';
B=[1,3,5;2,9,0]';
Cs = [];
index = 0;
for i = 1:length(A(1,:))
for j = 1:length(B(1,:))
index += 1;
Cs(:,:,index) = [A(:,i), B(:,j)];
end
end
Cs
Upvotes: 1