How to take average/median of table columns without the zeros?

1 view (last 30 days)
Hey there, I have a big table (called pm2d), 12 columns by 31 rows, and i need to take the average and median of each column without the zeros being factored in. I know i can use for loop and if statement to check the number of valid data points and get a new array with only valid data points... But every code i try to write is not working. Any assistance would be appreciated! Thanks!

Accepted Answer

Adam
Adam on 22 Feb 2017
Edited: Adam on 22 Feb 2017
pm2d( a ) ./ sum( logical( pm2d ) );
will give you the mean without taking zeros into account.
If you have a recent version of Matlab then
pm2d( pm2d == 0 ) = NaN;
medianRes = median( pm2d, 'omitnan' );
will give the median (you can actually do the same thing with 'mean' too).

More Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox 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!