# How can I ignore 0 values when computing the mean over the third dimensional of a 3D matrix?

54 views (last 30 days)
A LL on 11 Oct 2020
Commented: Rik on 11 Oct 2020
Hi,
I have a 3D matrix size 951x301x720 let call it A.
I want to get a 951x301 matrix B with the mean of A over the third dimension.
I computed the mean over the third dimension with B = mean(A,3) but I have a lot of zeros in my matrix and I do not want the mean to be affected by the 0 values.
How can I ignore the 0?
Thank you
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Asad (Mehrzad) Khoddam on 11 Oct 2020
You can set 0 values to NaN. Then find the mean of non NaN values;
Ap=A;
Ap(Ap==0)=NaN;
B=mean(Ap,3,'omitnan')
##### 4 CommentsShowHide 3 older comments
Rik on 11 Oct 2020
Yes, that is what I meant, thank you for correcting it.

Sign in to comment.

### Categories

Find more on Multidimensional Arrays 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!