How to take 2D mean ignoring nan values using cellfun?

조회 수: 11 (최근 30일)
Niky Taylor
Niky Taylor 2021년 3월 1일
댓글: Allen 2021년 5월 26일
Hi,
I am trying to write a code that takes a large square image, breaks it up into small square segments, and then calculates the mean and standard deviation of each segment ignoring nan values.
Right now I'm trying to use cellfun to do this. I have the following code:
% splitting large matrix into segments
a = mat2cell(A,dimSpec1,dimSpec1);
% calculating standard deviation in segments
sigma = cellfun(@std2,a,'UniformOutput',true);
% calculating standard deviation in segments
xbar = cellfun(@mean2,a,'UniformOutput',true);
The problem is that most of the small segments have nan values, so when I calculate sigma or xbar I end up with a matrix full of nan values. But when I use something like nanmean or mean with ignore nan, I can't do the calculation in 2d. So how can I calculate the 2D mean and std, ignoring nans, in cellfun?
I don't use cellfun before so apologies if this is a overly basic question. It's also very likely that I'm approaching the whole problem badly, so if anyone has any suggestions I'm more than happy to hear them. Thanks!

답변 (2개)

Allen
Allen 2021년 3월 1일
편집: Allen 2021년 5월 26일
Try the following:
a = mat2cell(A,dimSpec1,dimSpec1);
% calculating standard deviation in segments
sigma = cellfun(@(x) std(x,0,[1,2],'omitnan'),a,'UniformOutput',true);
% calculating standard deviation in segments
xbar = cellfun(@(x) mean(x,'all','omitnan'),a,'UniformOutput',true);
  댓글 수: 3
Niky Taylor
Niky Taylor 2021년 3월 1일
편집: Niky Taylor 2021년 3월 1일
Aha, it seems like what you suggested works for mean. For std I changed it to:
sigma = cellfun(@(x) std(x,0,[1 2],'omitnan'),a,'UniformOutput',true);
after taking a look at the documentation. Thanks!
Allen
Allen 2021년 5월 26일
Nice catch. I forgot that std uses a slightly different input syntax from mean. I made a correction on that line. If I was able to provide some help, please do not forget to accept the answer. This will help other people that are looking for a similar answer know what works.
Thanks,
Allen

댓글을 달려면 로그인하십시오.


Allen
Allen 2021년 3월 1일
Niky,
My previous suggestion did not fully look at the input requirements for 2D usage of std. The following revision should accomplish what you needing.
a = mat2cell(A,dimSpec1,dimSpec1); % Your inputs
% calculating standard deviation in segments
sigma = cellfun(@(x) std(x,0,'all','omitnan'),a,'UniformOutput',true);
% or
% cellfun defaults to uniform output, so no need to include for these uses
sigma = cellfun(@(x) std(x(:),'omitnan'),a);
% calculating standard deviation in segments
xbar = cellfun(@(x) mean(x,'all','omitnan'),a);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by