Any suggestion to replace the diagonal values of matrices in a 3D array?
古いコメントを表示
Any suggestion to replace the diagonal values of matrices in a 3D array?
in case1 to "0"
in case1 to "NaN"
e.g. for NaN
M_3D=randi(100, 4,4,3); %fake input
N=size(M_3D,1);
mask=1:N+1:end)=nan;
M_3D=M_3D.*mask;
4 件のコメント
What do you mean by "diagonal" in this context? If I had:
A = reshape(1:27, [3 3 3])
Do you want to set elements 1, 5, 9, 10, 14, 18, 19, 23, and 27 to 0? This would be setting the elements along the diagonal of each page of the 3-d array to 0.
Or do you just want to set elements 1, 14, and 27 to 0? This would be setting elements along a line "connecting opposite corners" (or the equivalent if your array isn't a cube) to 0.
Stephen23
2023 年 11 月 6 日
@Steven Lord: out of curiosity, how would you do a "connecting opposite corners" for an arbitrary number of dimensions?
One possibility is points at subscripts n*ones(1, ndimsOfArray) for n = 1:min(size(theArray)). Once you reach any "edge" of the array you stop.
So for an array of size (4, 3, 5) those would be the elements at subscripts (1, 1, 1), (2, 2, 2), and (3, 3, 3). That matches conceptually the behavior of the diag function on non-square matrices.
A = reshape(1:12, 3, 4)
diag(A) % (1, 1), (2, 2), and (3, 3) since min(size(A)) is 3.
julian gaviria
2023 年 11 月 6 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!