Create main diagonal with repeated numbers but one exception
이전 댓글 표시
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개)
카테고리
도움말 센터 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!