필터 지우기
필터 지우기

How to make a two-dimensional mask act on a three-dimensional array?

조회 수: 23 (최근 30일)
Here's a dummy three-dimensional array 'B' to illustrate the problem:
A = 9* ones (6,4,3); % create a 3 dimensional array
A(4,3,2)=NaN; % add a NaN
A(3,2,1)=NaN; % add another NaN;
B = A;
B is a 6x4x3 array with two NaN elements.
Wherever a NaN appears in array B, I want a NaN to overwrite all elements in the corresponding first dimension. So, in the example above, I want to convert B so that my output is
B(:,:,1) =
9 NaN 9 9
9 NaN 9 9
9 NaN 9 9
9 NaN 9 9
9 NaN 9 9
9 NaN 9 9
B(:,:,2) =
9 9 NaN 9
9 9 NaN 9
9 9 NaN 9
9 9 NaN 9
9 9 NaN 9
9 9 NaN 9
B(:,:,3) =
9 9 9 9
9 9 9 9
9 9 9 9
9 9 9 9
9 9 9 9
9 9 9 9
The script below appears to work but is not very elegant. Is there a way to do it without the 'for' loop?
% Here is my attempt to get NaN to overwrite all elements along the corresponding FIRST dimension
C = isnan(B); % create a logical mask
D = any (C,1); % look for any NaNs along the first dimension
for k=1:(size(B,1))
E=squeeze(B(k,:,:));
E(squeeze(D))=NaN; % apply the 'any' mask
B(k,:,:)=E ;
end
There is a very similar question here: https://uk.mathworks.com/matlabcentral/answers/358514-replace-all-the-array-with-nan-if-any-of-the-value-is-nan but I couldn't successfully tailor Jan's solution to my question. Thank you.

채택된 답변

Stephen23
Stephen23 2022년 5월 26일
"I wondered if there was any neat trick to do it by array multipication..."
A = 9* ones (6,4,3); % create a 3 dimensional array
A(4,3,2) = NaN; % add a NaN
A(3,2,1) = NaN; % add another NaN;
B = A
B =
B(:,:,1) = 9 9 9 9 9 9 9 9 9 NaN 9 9 9 9 9 9 9 9 9 9 9 9 9 9 B(:,:,2) = 9 9 9 9 9 9 9 9 9 9 9 9 9 9 NaN 9 9 9 9 9 9 9 9 9 B(:,:,3) = 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
X = 0./~any(isnan(B),1);
B = B+X
B =
B(:,:,1) = 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 B(:,:,2) = 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 B(:,:,3) = 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
If you really want to use multilplication add 1 to X.
  댓글 수: 1
Stephen23
Stephen23 2022년 7월 16일
편집: Stephen23 2022년 7월 16일
A = 9*ones(4,3,6); % create a 3 dimensional array
A(3,2,4) = NaN; % add a NaN
A(2,1,3) = NaN % add another NaN;
A =
A(:,:,1) = 9 9 9 9 9 9 9 9 9 9 9 9 A(:,:,2) = 9 9 9 9 9 9 9 9 9 9 9 9 A(:,:,3) = 9 9 9 NaN 9 9 9 9 9 9 9 9 A(:,:,4) = 9 9 9 9 9 9 9 NaN 9 9 9 9 A(:,:,5) = 9 9 9 9 9 9 9 9 9 9 9 9 A(:,:,6) = 9 9 9 9 9 9 9 9 9 9 9 9
B = A;
X = 0./~any(isnan(B),3);
B = B+X % ^ you just need to change this
B =
B(:,:,1) = 9 9 9 NaN 9 9 9 NaN 9 9 9 9 B(:,:,2) = 9 9 9 NaN 9 9 9 NaN 9 9 9 9 B(:,:,3) = 9 9 9 NaN 9 9 9 NaN 9 9 9 9 B(:,:,4) = 9 9 9 NaN 9 9 9 NaN 9 9 9 9 B(:,:,5) = 9 9 9 NaN 9 9 9 NaN 9 9 9 9 B(:,:,6) = 9 9 9 NaN 9 9 9 NaN 9 9 9 9

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

추가 답변 (1개)

Voss
Voss 2022년 5월 25일
편집: Voss 2022년 5월 25일
You can replicate your matrix D=any(isnan(B),1) in the first dimension using repmat, generating a 3D logical array the same size as B, and then use that 3D logical array as a logical index in B:
A = 9* ones (6,4,3); % create a 3 dimensional array
A(4,3,2)=NaN; % add a NaN
A(3,2,1)=NaN; % add another NaN;
B = A;
B(repmat(any(isnan(B),1),[size(B,1) 1 1])) = NaN;
disp(B);
(:,:,1) = 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 (:,:,2) = 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 9 9 NaN 9 (:,:,3) = 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
  댓글 수: 2
Steve Francis
Steve Francis 2022년 5월 25일
Thanks very much for your quick response. I wondered if there was any neat trick to do it by array multipication but your way works well.
Steve Francis
Steve Francis 2022년 7월 16일
I've changed the orientation of the matrix and was hoping that you could advise on the revised repmat statement, please?
A = 9* ones (4,3,6); % create a 3 dimensional array
A(3,2,4)=NaN; % add a NaN
A(2,1,3)=NaN; % add another NaN;
B = A;
The desired output is now
val(:,:,1) =
9 9 9
NaN 9 9
9 NaN 9
9 9 9
val(:,:,2) =
9 9 9
NaN 9 9
9 NaN 9
9 9 9
val(:,:,3) =
9 9 9
NaN 9 9
9 NaN 9
9 9 9
val(:,:,4) =
9 9 9
NaN 9 9
9 NaN 9
9 9 9
val(:,:,5) =
9 9 9
NaN 9 9
9 NaN 9
9 9 9
val(:,:,6) =
9 9 9
NaN 9 9
9 NaN 9
9 9 9
The following seems to work:
B(repmat(any(isnan(B),3),[1 1 size(B,3)])) = NaN;
Is that correct?

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by