Storing a forloop in a matrix
3 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Geoff Hayes
on 6 Dec 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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!