i have a column vector of 100 rows and would like to calculate its progressive median and mean for example (at the first row, only consider that row; at second row, consider first 2 rows; at third row, consider the first 3 rows and so on upto 100th row). I have used a for loop with sum/i to calculate the mean but I have failed to break through in calculating median. Kindly provide a solution or any alternatives.

4 commentaires

Fangjun Jiang
Fangjun Jiang le 17 Juil 2020
Can't you use mean() and median()?
Anita Nabatanzi
Anita Nabatanzi le 17 Juil 2020
when i use mean() and median() it calculates for all 100 samples at a go yet I need a value at each stage.
Rachel Surridge
Rachel Surridge le 17 Juil 2020
Does your data have more than one column?
Anita Nabatanzi
Anita Nabatanzi le 17 Juil 2020
working with one column for now, will expand it out to the rest of the matrix if successful at this stage.

Connectez-vous pour commenter.

 Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 17 Juil 2020
Modifié(e) : Fangjun Jiang le 17 Juil 2020

0 votes

M=100;
data=(1:M).';
MeanData=data;
MedianData=data;
for k=1:M
MeanData(k)=mean(data(1:k));
MedianData(k)=median(data(1:k));
end

4 commentaires

Anita Nabatanzi
Anita Nabatanzi le 17 Juil 2020
thank you Jiang, checked this for randomly generated data and it works, will apply it to my larger data, thanks.
Rogers Tuyisenge
Rogers Tuyisenge le 1 Août 2020
i wish to solve a similar problem but for a 600x200 matrix (data) and my code is not far from what Fangjun Jiang has. I am using nested for loops so that the calculation is done for all 600 rows but my results are incorrect. 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
Bruno Luong
Bruno Luong le 1 Août 2020
You miss the row index in the data
Mean_answer(k,h)= mean(data(k,1:h))
Rogers Tuyisenge
Rogers Tuyisenge le 1 Août 2020
Thanks mate, this fixes the problem!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by