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 Comments

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

Sign in to comment.

 Accepted Answer

Fangjun Jiang
Fangjun Jiang on 17 Jul 2020
Edited: Fangjun Jiang on 17 Jul 2020
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 Comments

thank you Jiang, checked this for randomly generated data and it works, will apply it to my larger data, thanks.
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
You miss the row index in the data
Mean_answer(k,h)= mean(data(k,1:h))
Thanks mate, this fixes the problem!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!