Create a Matrix with a specific main diagonal
显示 更早的评论
I want to create a matrix of all one's with the main diagonal of value n.
采纳的回答
更多回答(3 个)
dpb
2015-11-30
Multiple ways possible; one--
>> N=5; % diagonal value
>> sz=3; % size
>> m=ones(sz)+diag(repmat(N-1,1,sz))
m =
5 1 1
1 5 1
1 1 5
>>
doc diag % for details on optional arguments for different resulting types
One very simple solution:
>> X = +~eye(4);
>> X(X==0) = 5
X =
5 1 1 1
1 5 1 1
1 1 5 1
1 1 1 5
Andrei Bobrov
2015-11-30
d = randi(35,5,1);
out = ~eye(5) + diag(d);
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!