How to find the average in a row?

2 visualizaciones (últimos 30 días)
Alexander Nicholas
Alexander Nicholas el 17 de Abr. de 2017
Comentada: Alexander Nicholas el 17 de Abr. de 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 comentario
dpb
dpb el 17 de Abr. de 2017
Start with <Getting-started-with-matlab>, particularly the sections <matrices-and-arrays> and then specifically for your immediate problem <Array-indexing>

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 17 de Abr. de 2017
What's wrong with
mn=mean(tempv); % ???
  1 comentario
Alexander Nicholas
Alexander Nicholas el 17 de Abr. de 2017
This works. Thanks a lot. Very new to MATLAB

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by