MATLAB: How to access and modify only the non diagonal entries in a matrix

matrix

hello.. i hv a question. what is the command to call the non diagonal entries of a matrix? tq very much…

Best Answer

Please try the following:
M = 8;
N = 5;
X = randn(M,N);
idx = eye(M,N);
Y = (1-idx).*X;
Z = X(~idx);
HTH.
Best, Rick
Responding to your comment:
To multiply the non-diagonal elements by 2, please try:
A = [2 3 5;3 6 8;5 8 4];
idx = eye(size(A));
idx = idx + 2*(1-idx);
Y = idx.*A;
HTH.
Best, Rick