Reputation: 557
I have a matlab code that generate different combination between matrix. I am using it as part of a bigger script. Below is just example
A=[1,2,3,4];
B=[1,2,3,4];
for i=1:size(A,2);
for j=1:size(B,2);
C=[A(1,i),B(1,j)]
end
end
It will generate different possible combination between Matrix A and Matrix B as below.
C =
1 1
C =
1 2
C =
1 3
C =
1 4
C =
2 1
C =
2 2
C =
2 3
C =
2 4
C =
3 1
C =
3 2
C =
3 3
C =
3 4
C =
4 1
C =
4 2
C =
4 3
C =
4 4
But in my workspace variable, C shows only (4,4) which is the last answer. How to do if I want to get all C answer in one big matrix as
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
.... etc (which will be 16,2 matirx). Thanks.
Upvotes: 0
Views: 55
Reputation: 146
You can concatenate the matrix by using C=[C;A(1,i),B(1,j)];
. Of course,that requires C
to be initialized as an empty matrix. In your case, the final code is:
A=[1,2,3,4];
B=[1,2,3,4];
C=[];
for i=1:size(A,2);
for j=1:size(B,2);
C=[C;A(1,i),B(1,j)];
end
end
Upvotes: 2