Creating a diagonal matrix
3 次查看(过去 30 天)
显示 更早的评论
I will like to create such diagonal matrix. A=[1 1 1 0 0 0; 0 1 1 1 0 0; 0 0 1 1 1 0; 0 0 0 1 1 1] Appreciate your kind assitance.
0 个评论
回答(4 个)
John D'Errico
2017-8-5
编辑:John D'Errico
2017-8-5
That is not a diagonal matrix, not by the common definition of the term. It has non-zero diagonals, but that is as far as I might go.
Still trivial though.
triu(tril(ones(4,6),2))
ans =
1 1 1 0 0 0
0 1 1 1 0 0
0 0 1 1 1 0
0 0 0 1 1 1
Or...
toeplitz([1 0 0 0]',[1 1 1 0 0 0])
ans =
1 1 1 0 0 0
0 1 1 1 0 0
0 0 1 1 1 0
0 0 0 1 1 1
0 个评论
Andrei Bobrov
2017-8-5
编辑:Andrei Bobrov
2017-8-6
full(spdiags(ones(4,3),0:2,4,6))
or
t = true(4,6);
out = tril(t,2) & triu(t)
or
a = triu(ones(4,3));
out = [a,rot90(a,2)]
0 个评论
另请参阅
类别
在 Help Center 和 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!