How to create triangle inside matrix?

What if I wanted a triangle of numbers from the formula (2n-1) that resulted in a triangle inside a matrix such that...
[0 0 0 0 1 0 0 0 0]
0 0 0 2 1 2 0 0 0
0 0 3 2 1 2 3 0 0
0 4 3 2 1 2 3 4 0]
etc?...

 采纳的回答

sort(spdiags(toeplitz(1:n)))

4 个评论

Stop being a genius. :-)
What if I want to write a loop script to do the same thing?
Hi Billy!
With loop:
a = zeros(n,2*n-1);
for jj = 1:n
a(jj,n-jj+1:n+jj-1) = [jj:-1:1,2:jj];
end
Thank you! You are amazing!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by