Create main diagonal with repeated numbers but one exception
12 views (last 30 days)
Show older comments
Hello. Im new to Matlab and programming in general so i hope this is not to dumb.
I want to create a main diagonal line to my ones(5) matrix.
The diagonal should contain the first number as 1 and the rest are all 2.
Like this:
1 1 1 1 1
1 2 1 1 1
1 1 2 1 1
1 1 1 2 1
1 1 1 1 2
Since we are also supposed to construct bigger matrices of the same kind, i would like to be able to get a "repeat" on the number 2 so i dont have to write all of them out.
I have only been able to construct a diagonal that changes ALL the values with 2 by doing like this:
M = ones(5);
u = 2;
v = repmat(u,5,1);
M = M - diag(diag(M)) + diag(v)
2 1 1 1 1
1 2 1 1 1
1 1 2 1 1
1 1 1 2 1
1 1 1 1 2
So i need some way to get 1 instead of 2 in position (1,1). Is it possible to somehow integrate that into v or somewhere else and still keep the 'repmat'?
Thank you.
0 Comments
Accepted Answer
More Answers (0)
See Also
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!