Using For Loop to find average of columns in a matrix
Show older comments
Hi I have a 4x365 matrix.
I'd like to know the average of the numbers in each column, so that's 365 values I'm looking for.
I can't use the mean() or sum() commands, and I'm trying to get this done using for loop.
X=ones(4,365)
cols=size(X,2);
for i=1:4
for j=1:cols
sum = sum + X(i,j);
end
end
this gives me the final iteration. I need each iteration outputted into a new matrix so I may take the average.
Accepted Answer
More Answers (2)
madhan ravi
on 12 Nov 2018
m=rand(4,365);
mean(m,1)
2 Comments
madhan ravi
on 12 Nov 2018
you don't need to use loop to do the above
Emmanuel Limaco
on 16 Dec 2020
Some assignments specifically instruct the student to use a for-loop and not the mean() command
KSSV
on 12 Nov 2018
A = rand(4,365) ;
thesum = zeros(1,365) ;
for i = 1:4
thesum = thesum+A(i,:) ;
end
iwant = thesum/4 ;
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!