Storing Matrices from a for loop

1 vue (au cours des 30 derniers jours)
mattzoe
mattzoe le 8 Juin 2019
Commenté : madhan ravi le 8 Juin 2019
Hello everyone,
I want to multiply a matrix with a vector, in a for loop. In this case q is a continuous variable.
for q = 0:2:8;
B = [q, sin(q) ;
cos(q), 3*q ;
q, cos(q)]
h = [q; sin(q); 4]
A = B.' * h
end
Now my problem is, that I don't know how to store the result of B, h and A of each loop.
Or is there another solution without a for loop to fill the matrix B with each step? That would be easier for my code.
Best regards

Réponse acceptée

Jos (10584)
Jos (10584) le 8 Juin 2019
One option is to use an index to loop over the values, like this. You can than directly use that index to create a cell array to store the individual matrices A B and h. Like this
Qlist = 0:2:8
for k = 1:numel(Qlist)
q = Qlist(k) ;
B{k} = [q, sin(q) ;
cos(q), 3*q ;
q, cos(q)]
h{k} = [q; sin(q); 4]
A{k} = B{k}.' * h{k}
end
  1 commentaire
madhan ravi
madhan ravi le 8 Juin 2019
Also variables could be preallocated.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by