Why am I getting a red message when trying to run this code.
Show older comments

I ran this code earlier, I can not find out why it is giving me an error.
Accepted Answer
More Answers (1)
In the future, please don't post a picture of your code, post your code itself (formatted using the first button of the Code section of the message editor's toolstrip.)
Your line 39 is incorrect. Compare what you wrote (with the semicolon removed, so you can see what it created):
x0 = randn(10:1)
with what you likely meant:
x0 = randn(10,1)
Using 10:1 attempts to create an array of that size, which results in you getting an empty array.
10:1
If you'd flipped the two sizes it would "work" but you'd be creating a much larger array.
x1 = rand(1:10); % 10-dimensional!
x2 = rand(1,10);
whos x0 x1 x2
For generality, I recommend you also avoid hard-coding sizes like 10. If you modified your A you'd also have to modify everywhere your code assumes A has 10 columns. If instead you computed the sizes of x0 and other intermediate arrays using the size of A your code is more flexible.
A = rand(5, 6);
numrows = size(A, 1) % or could use height(A)
numcols = size(A, 2) % or could use width(A)
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!