How can I get the diagonal elements of a matrix to be 0's

How can I get the diagonal elements of a matrix to have all 0's and 1's everywhere else?

More Answers (1)

x=~eye(N);

5 Comments

Note: that would be a result of type logical rather than a numeric type. x = double(~eye(N)) would be double precision.
That wasn't a a given constraint! :) But, yes, is so...
"x = double(~eye(N)) would be double precision"
or
x = ~eye(N)+0
is what I'd probably actually write, being in favor of minimal keystrokes as a general rule...no idea which might have a performance advantage for increasing N.
I would probably use +0 as well, but writing double() explicitly is clearer to people about what is being done and why. People tend to expect that adding 0 will leave everything unchanged...
In the forum if I'd bothered to do the conversion I'd've added a comment on the purpose. As was, I figured the expression alone did satisfy the question and the remainder could be "exercise for the student"...

Sign in to comment.

Tags

Asked:

on 6 Jan 2016

Commented:

dpb
on 8 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!