Storing a forloop in a matrix

2 次查看(过去 30 天)
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!

采纳的回答

Geoff Hayes
Geoff Hayes 2015-12-6
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by