MATLAB: Matrix values from loop to raw vector

for loopmatrix array

Hello guys,
I have been trying other people suggestion, but still cannot create a vector of TEN 4×4 matrixes (also seen as a 4X40 matrix), that I should obtain by iteration for n=1:10
I just want to be able to obtain a final matrix that should be 4X40 but I keep on getting a matrix that is 10×40, which I don't understand 🙁
q=0.1;
L=2.5;%m
p=0.52;
c1=0;
c2=(1:10);
C=zeros(4,40)
for n=(1:10)
C(n,:)=[c2(n)+c2(n) -c2(n) -c2(n) (p*c2(n)-(1-p)*c2(n))*L; -c2(n) c1+c2(n) 0 -p*c2(n)*L; -c2(n) 0 c1+c2(n) (1-p)*c2(n)*L;(p*c2(n)-(1-p)*c2(n))*L -p*c2(n)*L (1-p)*c2(n)*L p^2*L^2*c2(n)+(1-p)^2*L^2*c2(n)]
end

Best Answer

I've fixed your code:
clear;
q=0.1;
L=2.5;%m

p=0.52;
c1=0;
c2=(1:10);
C=zeros(4,40);
for n=(1:10)
C(:, 4*(n-1)+1:4*n) = [c2(n)+c2(n), -c2(n), -c2(n), (p*c2(n)-(1-p)*c2(n))*L; -c2(n), c1+c2(n), 0, -p*c2(n)*L; -c2(n), 0, c1+c2(n), (1-p)*c2(n)*L;(p*c2(n)-(1-p)*c2(n))*L, -p*c2(n)*L, (1-p)*c2(n)*L, p^2*L^2*c2(n)+(1-p)^2*L^2*c2(n)];
end
Note 1: Use comma instead of space for separator.
Note 2: I indexed matrix C in a different way.
If you want to "create a vector of TEN 4x4 matrices", then use cell object. Later you can still convert it to matrix form using function cell2mat() if you want to.
clear;
q=0.1;
L=2.5;%m
p=0.52;
c1=0;
c2=(1:10);
C=cell(1,10);
for n=(1:10)
C{1,n}=[c2(n)+c2(n), -c2(n), -c2(n), (p*c2(n)-(1-p)*c2(n))*L; -c2(n), c1+c2(n), 0, -p*c2(n)*L; -c2(n), 0, c1+c2(n), (1-p)*c2(n)*L;(p*c2(n)-(1-p)*c2(n))*L, -p*c2(n)*L, (1-p)*c2(n)*L, p^2*L^2*c2(n)+(1-p)^2*L^2*c2(n)];
end
M = cell2mat(C);