MATLAB: How to calculate the average without taking zeros values

live scriptmatrixscript

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

Best Answer

[ii,~,v] = find(Y);
out = accumarray(ii,v,[],@mean);