matrix store issue for loop iteration

for i=1:3
------------
r=[s m]
The S and M matrix are 1 by 3 matrix. The result "r" matrix is 3 by 2.
The code above is generates combines 2 matrices that is S and M matrix. It generate r matrix output at i=1 upto i =3, but the only last value outputs are stored in "r" matrix that is for i=3.

Answers (1)

r = cell(1,3) ;
for i = 1:3
r{i} = [s m]
end
If You feel r will have same size m*p in every iteration, you can initilaize this as a 3D matrix.
r = zeros(m,p,3) ;
for i = 1:3
r(:,:,i) = [s m] ;
end

Categories

Asked:

on 9 Oct 2020

Answered:

on 9 Oct 2020

Community Treasure Hunt

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

Start Hunting!