MATLAB: Finding mean of matrix excluding 0

arraycell arraymatrixmatrix arraymeannonzero

I have a 4 1200×1200 matrixes which includes numerous 0 values, lets just call them 'a' 'b' 'c' and 'd'
How do i find the mean of each individual point in the matrix (that is a+b+c+d), while ignoring the 0 values?
Thank you for the help!

Best Answer

If I understand correctly what you want to do, this will work:
a = randi([0 5], 10);
b = randi([0 5], 10);
c = randi([0 5], 10);
d = randi([0 5], 10);
abcd = cat(3, a, b, c, d); % Concatenate Along 3rd Dimension
abcd(abcd == 0) = NaN; % Set Zeros To ‘NaN’
mean_abcd = mean(abcd, 3, 'omitnan'); % Use 'omitnan' Argument And Take ‘mean’ Across 3rd Dimension