How to combine multiple nx1 size of matrix into one matrix

As topic mentioned, i want to combine a matrix from many nx1 size matrix, i would like to have a script
Many Thnkas, Alex

댓글 수: 3

Can you explain with an example?
For example, I got matrix A, A=[1;2;3], and B, B=[4;5;6;7]. I want to combine them to be matrix C, C=[1;2;3;4;5;6;7], but now i got many matrix with differnet number of rows. How can i combine them ? Thanks!
Hi! You can reference my code! Good lucky! >> a{1} = [1 1 1;1 1 1]; >> a{1}
ans =
1 1 1
1 1 1
>> a{2} = [2 2 2;2 2 2]; >> a{3} = [3 3 3]; >> a{4} = [4 4 4;4 4 4;4 4 4]; >> a{5} = [5 5 5;5 5 5]; >> A = vertcat(a{:})
A =
1 1 1
1 1 1
2 2 2
2 2 2
3 3 3
4 4 4
4 4 4
4 4 4
5 5 5
5 5 5

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2014년 8월 12일
could you plz exlpain more about vertcat(matrices{:}); by showing an example.
>> matrices={[1,2;3 4],[5,6;7,8]};
>> vertcat(matrices{:})
ans =
1 2
3 4
5 6
7 8

댓글 수: 6

Sorry, i want to clarify that now i am using a loop to generate matrices out and then store them into a new matrix .Can i still use vertcat?
Matt J
Matt J 2014년 8월 12일
편집: Matt J 2014년 8월 12일
Yes, store the i-th vector to matrices{i} in the loop and then when the loop is finished, use vertcat as above.
However, Matlab appears this one (In an assignment A(:) = B, the number of elements in A and B must be the same.) , how can i fix it ?
Matt J
Matt J 2014년 8월 12일
편집: Matt J 2014년 8월 12일
Sounds like you are not using cell array indexing. If you don't know what that means, you have some reading to do I'm afraid,
Ok! Thanks !
The first step to fix it is to show the code which is throwing this error ;-)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Joakim Magnusson
Joakim Magnusson 2014년 8월 12일
편집: Joakim Magnusson 2014년 8월 12일
Where do you have your matrices? If you can get them into cell arrays like this:
matrices = cell(1,2);
matrices{1} = A;
matrices{2} = B;
C = [];
Then you could do like this:
for i = 1:size(matrices,2)
C = [C ;cell2mat(matrices(i))]
end
I'm not sure how you want to combine your matrices but hope this was at least a little helpful.

댓글 수: 4

If A, B, ... are in a cell array, you wouldn't merge them by a loop but rather by cell2mat(matrices').
Matt J
Matt J 2014년 8월 12일
편집: Matt J 2014년 8월 12일
cell2mat.m does use a loop. You could use vertcat, which is a builtin optimized function,
C=vertcat(matrices{:});
Matt , could you plz exlpain more about vertcat(matrices{:}); by showing an example. Thank You!
Thanks @Matt, I didn't use cell2mat a lot, so I didn't think about it too much. Your solution is very elegant.

댓글을 달려면 로그인하십시오.

Joakim Magnusson
Joakim Magnusson 2014년 8월 12일
편집: Joakim Magnusson 2014년 8월 12일
Sorry, i want to clarify that now i am using a loop to generate matrices out and then store them into a new matrix .Can i still use vertcat?
NG, do you mean like this?
B = [];
for i = 1:10
A = magic(4);
A(:, 2:4) = [];
B = vertcat(B, A);
end

댓글 수: 2

That would not be recommendable. With B growing inside the loop, you will be doing a lot of repeated memory re-allocation.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

NG
2014년 8월 12일

댓글:

2015년 10월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by