How to find the average in a row?
2 views (last 30 days)
Show older comments
Alexander Nicholas
on 17 Apr 2017
Commented: Alexander Nicholas
on 17 Apr 2017
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
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>
Accepted Answer
More Answers (0)
See Also
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!