How to find the mean and leave out the zeros?

411 ビュー (過去 30 日間)
Sam
Sam 2014 年 12 月 22 日
コメント済み: Pratyush Das 2020 年 8 月 23 日
I've got a matrix with 4 numbers (38,46,47,,0). I want to find the mean for the numbers 38, 46 and 47. I want to leave out the zero in my commando
My code:
mean(Gemiddelde_Heuphoek{welke_pp} = mean(Gemiddelde_gewrichtshoek.Heuphoek.meting)
%the code on the right of the equal sign contains the 4 numbers.
%the code on the left of the equal sign calculates the mean of the 4 numbers.
But I want to create a code who leaves out the zeros en calculates the mean for only the non-zero elements.

採用された回答

Sean de Wolski
Sean de Wolski 2014 年 12 月 22 日
mnz = mean(nonzeros(x))
  3 件のコメント
Image Analyst
Image Analyst 2014 年 12 月 22 日
Cool - I didn't know about the nonzeros functions.
Pratyush Das
Pratyush Das 2020 年 8 月 23 日
Thanks a bunch

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 12 月 22 日
Try this:
nonZeroIndexes = m ~= 0; % m is your row vector array of numbers.
theMean = mean(m(nonZeroIndexes));
m(nonZeroIndexes) selects only the non-zero numbers and so only those numbers will be considered by the mean() function.

Community Treasure Hunt

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

Start Hunting!

Translated by