필터 지우기
필터 지우기

To mirror (i.e, to copy) the upper matrix to the lower half in matrices arranged in 3D

조회 수: 2 (최근 30일)
These two lines mirror (i.e, copy) the upper matrix to the lower half in matrix M, and replace the diagonal elements of the same M with NaN values
M2=triu(M)+triu(M,1)';
%2. To replace the diagonal elements of A with NaN values
M2(eye(size(M2))==1) = nan;
How can I implement them in a 3D array? (e.g., M_3D=374x374x5). The code below does not work.
ns = size(M_3D,3);
M_3D_ = M_3D;
for s = 1:ns
thisM = M_3D(:,:,s);
thisM = triu(M_3D(:,:,s))+triu(M_3D,1)';
M_3D_(:,:,s) = thisM;
M3D_(eye(size(M_3D_(:,:,s)))==1) = nan;
end

채택된 답변

Matt J
Matt J 2023년 10월 23일
편집: Matt J 2023년 10월 23일
M_3D=randi(100, 4,4,3); %fake input
N=size(M_3D,1);
mask=triu(ones(N));
mask(1:N+1:end)=nan;
M_3D=M_3D.*mask;
M_3D=M_3D+pagetranspose(M_3D)
M_3D =
M_3D(:,:,1) = NaN 100 71 68 100 NaN 68 46 71 68 NaN 65 68 46 65 NaN M_3D(:,:,2) = NaN 68 99 23 68 NaN 50 29 99 50 NaN 45 23 29 45 NaN M_3D(:,:,3) = NaN 73 46 4 73 NaN 57 2 46 57 NaN 56 4 2 56 NaN
  댓글 수: 2
julian gaviria
julian gaviria 2023년 10월 23일
It nicely works. Thanks @Matt J
One last question. Do you mind to describe what you did in
mask(1:N+1:end)=nan;
M_3D=M_3D.*mask;
M_3D=M_3D+pagetranspose(M_3D)
Matt J
Matt J 2023년 10월 23일
It may help to look at the mask,
N=8;
mask=triu(ones(N));
mask(1:N+1:end)=nan
mask = 8×8
NaN 1 1 1 1 1 1 1 0 NaN 1 1 1 1 1 1 0 0 NaN 1 1 1 1 1 0 0 0 NaN 1 1 1 1 0 0 0 0 NaN 1 1 1 0 0 0 0 0 NaN 1 1 0 0 0 0 0 0 NaN 1 0 0 0 0 0 0 0 NaN

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by