How do i create a (n x n) matrix using algorithm or other way whose elements are either -1, +3, –3 & 1 , otherwise 0, such that a11=a22=a33=.....=-1, a12=a23=a34=.....= 3, a13=a24=a35=.......= -3, a14=a25=a36=....= 1,.

조회 수: 1 (최근 30일)
How do i create a (n x n) matrix using algorithm or other way, whose elements are either -1, +3, –3 & 1 ,such that a11=a22=a33=.....=-1, a12 = a23 = a34 =.....= 3, a13 = a24 = a35=.......= -3, a14 = a25 = a36 =....= 1,otherwise 0.

채택된 답변

Stephen23
Stephen23 2016년 1월 23일
편집: Stephen23 2016년 1월 23일
You can use toeplitz:
>> N = 6;
>> toeplitz([-1,zeros(1,N-1)],[-1,3,-3,1,zeros(1,N-4)])
ans =
-1 3 -3 1 0 0
0 -1 3 -3 1 0
0 0 -1 3 -3 1
0 0 0 -1 3 -3
0 0 0 0 -1 3
0 0 0 0 0 -1
If N can be less than four, then you will need to do this:
>> N = 3;
>> C = [-1,3,-3,1,zeros(1,N-4)];
>> R = [-1,zeros(1,N-1)];
>> toeplitz(R(1:N),C(1:N))
ans =
-1 3 -3
0 -1 3
0 0 -1
  댓글 수: 1
Stephen23
Stephen23 2016년 1월 24일
It is trivial to make this work with non-square matrices:
>> M = 5;
>> N = 7;
>> toeplitz([-1,zeros(1,M-1)],[-1,3,-3,1,zeros(1,N-4)])
ans =
-1 3 -3 1 0 0 0
0 -1 3 -3 1 0 0
0 0 -1 3 -3 1 0
0 0 0 -1 3 -3 1
0 0 0 0 -1 3 -3

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by