matrix values from loop to raw vector?

3 visualizzazioni (ultimi 30 giorni)
Hello guys,
I have been trying other people suggestion, but still cannot create a vector of TEN 4x4 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 10x40, 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

Risposta accettata

Peter Jarosi
Peter Jarosi il 18 Lug 2019
Modificato: Peter Jarosi il 18 Lug 2019
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);
  2 Commenti
Geraldine Ejimadu
Geraldine Ejimadu il 19 Lug 2019
Modificato: Geraldine Ejimadu il 19 Lug 2019
Thank You Peter!
I am going to try your suggestion.
Just to understand your reasoning:
what does "C(:, 4*(n-1)+1:4*n)" stand for in your first version of the solution?
Why did you put 1 in "C{1,n}" in your second version of the solution?
Edit: By the way I tried it and it works for mee too!
Thank you
Geraldine
Peter Jarosi
Peter Jarosi il 19 Lug 2019
Modificato: Peter Jarosi il 19 Lug 2019
Hi Geraldine,
You're very welcome!
In my first example C is a four by fourty matrix, and 4*(n-1)+1:4*n is just a tricky indexing of columns. If n=1 it equals 1:4, if n=2 it equals 5:8, and so on... if n=10 it equals 37:40.
In my second example C is a (1:10) size row vector of cells, where each cell contains a four by four matrix. That's why I used curly brackets indexing cell elements, for instance C{1,2} represents the second element in the first row (in this special case C has only one row), and this cell element is a 4x4 array of numbers. Open your variables from the workspace and look at them! Maybe C{2} is enough if we only have one row in cell C. I haven't tested it. You can try it but I always prefer using both row and column indexes in order to aviod possible bugs in my code.
I hope that I explained it understable and correctly.
Peter
p.s: Thank you for accepting my answer!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by