How can I save 3 matrices created by for loop to save in a single matrix?
4 次查看(过去 30 天)
显示 更早的评论
I have a for loop which outputs the following matrices after each iteration: [3,4;5,6], [8,5;2,4], [1,8;3,7]
I want to save these outputs into a single matrix and it should be this: final=[3,4;5,6;8,5;2,4;1,8;3,7]
The size of the 'final' matrix is known to me.
How do I do this?
0 个评论
采纳的回答
Rishabh Rathore
2018-6-5
编辑:Rishabh Rathore
2018-6-5
Since you know the size of the final matrix, first you can initialize the matrix of that size and then assign the appropriate row index.
for example the following code creates the desired matrix from given output of each iterations
matrix=zeros(6,2) % only 3 iteration creating 2*2 matrix.
for i=1:3
%your code%
output=x %2*2 matrix;
matrix((2*i-1),:)=output(1,:);
matrix(2*i,:)=output(2,:);
end
The easier way would be to just append the output of each iteration, but it could be slow as the final matrix won't be pre-allocated, but here's the code, in case you need it.
matrix=[];
for i=1:end
% your code
output=x %2*2 matrix;
matrix=[matrix; output];
0 个评论
更多回答(2 个)
deepak kumar
2018-6-5
a = [3,4,5,6]
b = [8,5;2,4]
c = [1,8;3,7]
d = vertcat(a,b,c)
or we can also do as temp =[]; for i =1:n %let say a have your output temp =[temp;a] end
0 个评论
Suraj Sudheer Menon
2020-6-22
let x,y,z be the 3 iteration output vectors.
ans=[x ; y ; z]
% This creates a new vector by combining the above vectors. The semi colon indicates seperate rows(vertically joining).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!