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?...

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 8월 30일

3 개 추천

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

댓글 수: 4

Billy Albritton
Billy Albritton 2016년 8월 30일
Stop being a genius. :-)
Billy Albritton
Billy Albritton 2016년 8월 30일
What if I want to write a loop script to do the same thing?
Andrei Bobrov
Andrei Bobrov 2016년 8월 30일
편집: Andrei Bobrov 2016년 8월 30일
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
Billy Albritton
Billy Albritton 2016년 9월 10일
Thank you! You are amazing!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

질문:

2016년 8월 30일

댓글:

2016년 9월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by