How to calculate the average without taking zeros values?
Mostra commenti meno recenti
Hi, I have a large data list which has this form:
Y=
1 2 4 5 6 7 0 0 0 8 9 0 0 0 0... AVerage1=5.25
2 3 4 5 6 7 0 0 0 8 0 0 0 9 0... Average2=5.5
3 4 5 6 7 8 9 0 0 0 0 0 0 0 0... Average3=6
.
.
.
I would like to calculate the average of each row wihout taking zeros values.
Thanks in advance
Risposta accettata
Più risposte (3)
Jos (10584)
il 13 Dic 2013
Modificato: Jos (10584)
il 13 Dic 2013
No need of a loop:
A = [1 2 3 ; 10 0 30 ; 9 0 0]
rowMean = sum(A,2) ./ sum(A~=0,2)
Note that zeros do not contribute to sum(A) …
4 Commenti
afrya
il 13 Dic 2013
David Du
il 28 Nov 2015
Very smart idea
Alvaro Espinoza Hernández
il 13 Ago 2018
Excellent!!!! Thanks
Mech Princess
il 5 Mar 2019
mean(nonzeros(X))
Y=[1 2 4 5 6 7 0 0 0 8 9 0 0 0 0; ...
2 3 4 5 6 7 0 0 0 8 0 0 0 9 0;...
3 4 5 6 7 8 9 0 0 0 0 0 0 0 0];
for i=1:3
mean(nonzeros(Y(i,:)))
end
Categorie
Scopri di più su Mathematics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!