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])
A =
A(:,:,1) = 1 4 7 2 5 8 3 6 9 A(:,:,2) = 10 13 16 11 14 17 12 15 18 A(:,:,3) = 19 22 25 20 23 26 21 24 27
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
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)
A = 3×4
1 4 7 10 2 5 8 11 3 6 9 12
diag(A) % (1, 1), (2, 2), and (3, 3) since min(size(A)) is 3.
ans = 3×1
1 5 9
julian gaviria
julian gaviria 2023 年 11 月 6 日
@Steven Lord, following you example, I want to replace the elements 1, 5, 9, 10, 14, 18, 19, 23, and 27 either by "0" or by "NaN" values. These are two different cases. Apologies for not providing all the info in the initial post. Sometimes my 3D matrices have NaN values in the diagonals, which I must replace by "0" value.s @Dyuman Joshi's suggestion worked out.
Thanks anyway.

サインインしてコメントする。

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 6 日
Replacing diagonal values of each 2D matrix of a 3D matrix with NaN
M = randi(100, 4,4,3); %fake input
N = eye(size(M,[1 2]));
M(M&N)=NaN
M =
M(:,:,1) = NaN 88 80 30 51 NaN 98 73 75 100 NaN 63 68 51 61 NaN M(:,:,2) = NaN 13 65 60 99 NaN 55 50 16 24 NaN 94 46 52 86 NaN M(:,:,3) = NaN 50 49 17 41 NaN 68 52 79 80 NaN 79 83 47 86 NaN

4 件のコメント

julian gaviria
julian gaviria 2023 年 11 月 6 日
Thank you @Dyuman Joshi,
Do you know any alternative when the diagonal values are NaNs? Your code did not work in that situation:
Error using &
NaN values cannot be converted to logicals.
In that case -
%fake input
M = randi(100, 4,4,3);
M(1) = NaN
M =
M(:,:,1) = NaN 7 27 89 63 89 61 21 59 79 68 44 66 45 17 61 M(:,:,2) = 99 92 2 33 55 25 24 31 21 46 88 39 75 79 12 39 M(:,:,3) = 47 6 69 13 70 30 51 49 6 44 85 63 23 27 97 91
N = eye(size(M,[1 2]));
M(ones(size(M))&N)=NaN
M =
M(:,:,1) = NaN 7 27 89 63 NaN 61 21 59 79 NaN 44 66 45 17 NaN M(:,:,2) = NaN 92 2 33 55 NaN 24 31 21 46 NaN 39 75 79 12 NaN M(:,:,3) = NaN 6 69 13 70 NaN 51 49 6 44 NaN 63 23 27 97 NaN
julian gaviria
julian gaviria 2023 年 11 月 6 日
It nicely worked! Thanks a lot @Dyuman Joshi
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 6 日
You are welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File 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