MATLAB: Substitue a diagonal matrix

diagonalmatrix

Hi all, I'm trying to create a square matrix 9×9 where all the values are 9 and the diagonal is 0. Example:
0 9 9
9 0 9
9 9 0
I found a way to do it which is:
s(logical(eye(size(s)))) = 0
Is there an easy way to substitute a diagonal of a matrix (possibly without using [m n] = size(s) –> s[1:m]….) Thanks

Best Answer

The one line solution to your problem that strikes me (without using size function) is as under
9*(~eye(9,9))
Copy the above line of code and run in your command window. It gives the result you want.
Vote my answer if it was helpful....