How can I can I create a square diagonal matrix and insert variables into it?
2 views (last 30 days)
Show older comments
Hello,
I need to create a 500x500 matrix that looks like this
[0 0 0 0 0 x
0 0 0 0 x 0
0 0 0 x 0 0
0 0 y 0 0 0
0 y 0 0 0 0
y 0 0 0 0 0 ]
now i know that i can use eye(0,0) command and use the fliplr() to create an inverse identity matrix. but i just need to know how to add variables into it.....
any ideas?
your help is appreciated
THANK YOU :)
2 Comments
Accepted Answer
Azzi Abdelmalek
on 31 Jan 2014
A=zeros(6)
[n,m]=size(A);
idx=sub2ind([n m],1:n,n:-1:1)
x=5;
y=10;
A(idx)=[x x x y y y ]
3 Comments
Azzi Abdelmalek
on 31 Jan 2014
A=zeros(500)
[n,m]=size(A);
idx=sub2ind([n m],1:n,n:-1:1)
x=5;
y=10;
A(idx)=[repmat(x,1,250) repmat(y,1,250) ]
More Answers (1)
Bjorn Gustavsson
on 31 Jan 2014
Unless you need something outlandishly fast or special I'd go for KISS:
M = flipud(diag([y y y y x x x x]));
HTH
4 Comments
Azzi Abdelmalek
on 31 Jan 2014
Using Bjorn code
M = flipud(diag([repmat(x,1,250) repmat(y,1,250)]));
See Also
Categories
Find more on Matrix Indexing 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!