How to find the average in a row?

2 views (last 30 days)
I have a row of values in:
tempv = [50, 60, 65, 30, 40, 66, 30, 55, 72, 72, 75, 50, 60, 65, 30, 40, ...
66, 30, 55, 63, 72, 75, 50, 60, 65, 30, 40, 66, 30, 55, 53];
I have to find the sum using a for loop. I tried the below but it is adding up the number of the slot, not the value.
x=length(tempv);
sum=0;
for i = 1 : length(tempv)
sum=sum+i;
end
y=sum/x;
fprintf('The average temperature in Fahrenheit was: %i\n', y)
fprintf('sum: %i\n', sum)
Command Window:
The average temperature in Fahrenheit was: 16
sum: 496
How do I find the average of these values in a for loop? I would assume there is some way to add each value one by one and store that in a variable. Then I would just have to divide that variable over the length(tempv). But I am not sure how to ge the sum of those values in a for loop
  1 Comment
dpb
dpb on 17 Apr 2017
Start with <Getting-started-with-matlab>, particularly the sections <matrices-and-arrays> and then specifically for your immediate problem <Array-indexing>

Sign in to comment.

Accepted Answer

dpb
dpb on 17 Apr 2017
What's wrong with
mn=mean(tempv); % ???

More Answers (0)

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!