Use kron to build a matrix

조회 수: 13 (최근 30일)
Jyoti Mangal
Jyoti Mangal 2021년 5월 24일
댓글: Stephen23 2021년 5월 25일
Hi,
I would like to build a matrix that resembles something like
A = [-1 1 0 0 0 0 0 0;
0 -1 1 0 0 0 0 0;
0 0 -1 1 0 0 0 0;
0 0 0 -1 1 0 0 0;
0 0 0 0 -1 1 0 0;
0 0 0 0 0 -1 1 0;
0 0 0 0 0 0 -1 1];
Can I use kron function to build something like this? One of the things that needs to be a variable will be the number of rows, so I need my kron function to be flexible such that it can build more such matrices, for eg. a matrix with four such rows would look like
A = [-1 1 0 0 0;
0 -1 1 0 0;
0 0 -1 1 0;
0 0 0 -1 1;]
Also happy to do using any other method than kron. Thank you for your help.

채택된 답변

Matt J
Matt J 2021년 5월 24일
No, the matrix you've shown is not a Kronecker product, but the matrix is easily generated using diff, e.g.,
A=full(diff(speye(7)))
A = 6×7
-1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1
  댓글 수: 2
Jyoti Mangal
Jyoti Mangal 2021년 5월 24일
Thank you so much! This worked like a charm
Stephen23
Stephen23 2021년 5월 25일
A = diff(eye(7)) % no need for sparse array
A = 6×7
-1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 -1 1

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

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 5월 24일
Hi,
In you exercise, use eye() and kron(). and then change the necessary element of the generated matrix if necessary.
Good luck

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by