Comparing multidimensional array with 2-D array

조회 수: 1 (최근 30일)
Carol Clayson
Carol Clayson 2020년 4월 11일
댓글: Ameer Hamza 2020년 4월 11일
I have an array with indices A(1388, 56, 720). I want to do a nanmean along the third dimension. If that was all, B = nanmean(A,3) would be fine. However, I actually only want to get a nanmean when the number of non-nan values in the third dimension are greater than 360 (or other number). I would like to avoid doing some kind of series of if statements going element by element through A.
I can find the number of non-nan elements easily at each point in the first two dimensions by:
C = sum(~isnan(A),3);
However, I can't figure out how to make that into something easily useful. For instance,
D = nanmean(A(C > 360),3);
produces a vector in D of less than 1388*56 points with no indication of where these should be in the A(1388,56) array.
Any help to make something out of this would be very much appreciated.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 11일
Something like this
A = rand(1388, 56, 720);
A(A<0.5) = nan; % making 50% elements nan
Aa = A; % make a copy of A
mask = repmat(sum(isnan(A), 3) < 360, 1, 1, size(A,3));
A(mask) = nan; % setting all the 3rd column values to nan so that nanmean ignore it.
B = nanmean(A,3); % nan indicate that this row column were ignored in 3rd dimension
B(isnan(B)) = 0;
  댓글 수: 2
Carol Clayson
Carol Clayson 2020년 4월 11일
Thank you very much! The key line here was the mask = repmat . . .
My only question was why you made Aa
Ameer Hamza
Ameer Hamza 2020년 4월 11일
In case, you need to use the value of A again later in code. If it is not required, then you can overwrite it.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Array Geometries and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by