calculate mean in a for loop

43 visualizaciones (últimos 30 días)
Rogers Tuyisenge
Rogers Tuyisenge el 1 de Ag. de 2020
Comentada: Rogers Tuyisenge el 1 de Ag. de 2020
i wish to calculate mean for every additional column (eg.for first row; calculate mean for column 1 store in (1,1), for column 2 calculate mean for first 2 columns store in (1,2), for column 3 calculate mean for first 3 columns store in (1,3) and so on) for each of my rows independently.
I have a 600x200 matrix (data) and I have tried using nested for loops so that the calculation is done for all 600 rows but my results are incorrect. grateful for any ideas. a part of my code looks like this;
m=600
n=200
for k=1:m
for h=1:n
Mean_answer(k,h)= mean(data(1:h))
end
end

Respuesta aceptada

Image Analyst
Image Analyst el 1 de Ag. de 2020
You need to specify the kth row of data
Mean_answer(k, h)= mean(data(k, 1:h))
  1 comentario
Rogers Tuyisenge
Rogers Tuyisenge el 1 de Ag. de 2020
Thanks mate, this fixes the problem!

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 1 de Ag. de 2020
Editada: Matt J el 1 de Ag. de 2020
No need for loops,
Mean_answer=cumsum(data,2)./(1:size(data,2));
  1 comentario
Rogers Tuyisenge
Rogers Tuyisenge el 1 de Ag. de 2020
Thanks Matt, working on other stats too so just wanted all of them in a loop

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by