Storing a forloop in a matrix

3 vues (au cours des 30 derniers jours)
Melissa2305
Melissa2305 le 6 Déc 2015
Commenté : Melissa2305 le 6 Déc 2015
Hi!
I am writing an optimization script at the moment, but encountered a problem. I am using a for-loop and I want it to store the result in one matrix. However, it overwrites the previous results, and therefore only presents one row as a result in the end.
My basic script:
data = xlsread('coal.xlsx', 'C2:C121')';
for ii=1:40
Rows = zeros(40,3);
Rows (ii,:) = data (ii:40:120)
end
Thanks in advance!

Réponse acceptée

Geoff Hayes
Geoff Hayes le 6 Déc 2015
Melissa - initialize Rows outside of the for loop because you are just overwriting all those values from previous iterations with zeros whenever you re-instantiate it at the first line of your for loop. Try
Rows = zeros(40,3);
for ii=1:40
Rows (ii,:) = data (ii:40:120)
end
Though I'm not sure that is exactly what you will need since it is unclear on the dimensions of data.
  1 commentaire
Melissa2305
Melissa2305 le 6 Déc 2015
Thanks, that was the problem!

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