In Matlab, how to replace the diagonal elements of a matrix with zeros?
With MATLAB, the simplest solution to replace the diagonal elements of a square matrix with zeros is to isolate the diagonal elements and to subtract these elements from the original matrix:
B = A - diag(diag(A))
Here is an example :
>> A=[1 2 3 ; 4 5 6 ; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> B = A - diag(diag(A))
B =
0 2 3
4 0 6
7 8 0
With MATLAB, the simplest solution to replace the diagonal elements of a square matrix with zeros is to isolate the diagonal elements and to subtract these elements from the original matrix:
B = A - diag(diag(A))
Here is an example :
>> A=[1 2 3 ; 4 5 6 ; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> B = A - diag(diag(A))
B =
0 2 3
4 0 6
7 8 0
With MATLAB, the simplest solution to replace the diagonal elements of a matrix with 0 is to isolate the diagonal elements and to substract these elements to the original matrix:
B = A - diag(diag(A))
Here is an example :
>> A=[1 2 3 ; 4 5 6 ; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> B = A - diag(diag(A))
B =
0 2 3
4 0 6
7 8 0
# | ID | Query | URL | Count |
---|