To mirror (i.e, to copy) the upper matrix to the lower half in matrices arranged in 3D
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
julian gaviria
am 23 Okt. 2023
Kommentiert: Matt J
am 23 Okt. 2023
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
0 Kommentare
Akzeptierte Antwort
Matt J
am 23 Okt. 2023
Bearbeitet: Matt J
am 23 Okt. 2023
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)
2 Kommentare
Matt J
am 23 Okt. 2023
It may help to look at the mask,
N=8;
mask=triu(ones(N));
mask(1:N+1:end)=nan
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!