How to combine multiple nx1 size of matrix into one matrix
Ältere Kommentare anzeigen
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 Kommentare
Azzi Abdelmalek
am 12 Aug. 2014
Can you explain with an example?
NG
am 12 Aug. 2014
tran thang
am 10 Okt. 2015
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
Akzeptierte Antwort
Weitere Antworten (3)
Azzi Abdelmalek
am 12 Aug. 2014
Bearbeitet: Azzi Abdelmalek
am 12 Aug. 2014
You mean concatenation
[A;B]
Read the documentation http://www.mathworks.com/help/matlab/math/creating-and-concatenating-matrices.html#f1-85019
Joakim Magnusson
am 12 Aug. 2014
Bearbeitet: Joakim Magnusson
am 12 Aug. 2014
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 Kommentare
Michael Haderlein
am 12 Aug. 2014
If A, B, ... are in a cell array, you wouldn't merge them by a loop but rather by cell2mat(matrices').
cell2mat.m does use a loop. You could use vertcat, which is a builtin optimized function,
C=vertcat(matrices{:});
NG
am 12 Aug. 2014
Michael Haderlein
am 12 Aug. 2014
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
am 12 Aug. 2014
Bearbeitet: Joakim Magnusson
am 12 Aug. 2014
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 Kommentare
Matt J
am 12 Aug. 2014
That would not be recommendable. With B growing inside the loop, you will be doing a lot of repeated memory re-allocation.
Joakim Magnusson
am 12 Aug. 2014
ah, okay!
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!