Extracting matrix diagonal with diag(A)

4 views (last 30 days)
Irina
Irina on 19 Feb 2015
Commented: Irina on 19 Feb 2015
A simple function that used to run now gives an error message. What am I doing wrong?
>> a = eye(4,4);
>> diag(a)
Subscript indices must either be real positive integers or logicals.
>>

Accepted Answer

Evan
Evan on 19 Feb 2015
Edited: Evan on 19 Feb 2015
Does this fix your problem?
a = eye(4,4) == 1;
diag(a)
While the diag function works fine, if you have a variable named diag (not a good idea--give it a unique name that isn't the same as a builtin function), it expects a logical, not double, input for indexing.
  1 Comment
Irina
Irina on 19 Feb 2015
Thank you, that's exactly what happened.

Sign in to comment.

More Answers (1)

Erik S.
Erik S. on 19 Feb 2015
Do you have another matrix in your workspace called diag?
  1 Comment
Irina
Irina on 19 Feb 2015
Thank you, that's exactly what happened.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!