help creating a matrix from data in a for loop.

I have
hrs = 24
Iterations/Hr = 60
for k = 1:hrs
for b = 1: iterations/hr
'conditions'
end
end
how do i take the data from the loops and put it into a 60x24 matrix in order to be able to find max, min, and average of each hour.

 采纳的回答

hrs = 24; % 24 hours
iterations = 60; % 60 iterations per hour
data = zeros(iterations,hrs); % initialize data to be a 60-by-24 matrix of zeros
for k = 1:hrs
for b = 1:iterations
data(b,k) = k*b; % some result based on 'conditions' (or whetever else)
end
end
% min, max, and average, by hour
min_by_hour = min(data);
max_by_hour = max(data);
avg_by_hour = mean(data);
% same thing, but explicitly saying to operate along the first dimension of data
min_by_hour = min(data,[],1);
max_by_hour = max(data,[],1);
avg_by_hour = mean(data,1);

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2021b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by