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.

 Accepted Answer

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);

More Answers (0)

Categories

Products

Release

R2021b

Tags

Asked:

on 26 Apr 2022

Answered:

on 26 Apr 2022

Community Treasure Hunt

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

Start Hunting!