how can I write this in a compact form? can anyone suggest a single line code for it

조회 수: 17 (최근 30일)
yita_mn = [
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1;
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1;
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
];

채택된 답변

John D'Errico
John D'Errico 2023년 1월 22일
편집: John D'Errico 2023년 1월 22일
At its heart, this is just a basic circulant matrix. So use a tool that will do that. I posted such a tool on the file exchange. But you can just use gallery.
n = 8; % an 8x8 circulant matrix of that form
M = gallery('circul',[0 1,zeros(1,n-3),1])
M = 8×8
0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0
Or you can view this as a Toeplitz matrix. Toeplitz matrices are a close second cousin to circulant matrices. Your matrix is always symmetric, so a symmetric Toeplitz matrix can be formed by just supplying the first row or column.
M2 = toeplitz([0 1,zeros(1,n-3),1])
M2 = 8×8
0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0
Of course there are other ways to do it. Since your matrix is actually quite sparse, you could build it as a sparse banded matrix. The virtue of that is if n is large, (as it often will be in applications like this) then you use efficient sparse storage, as well as efficient computations thereafter with that matrix.
I'll use n=50 here, but typically n might be a number in the thousands or more, if you are really needing to use a sparse matrix. 50 is large enough that you can visualize the banded structure easily, yet not too large that you cannot see the dots. (If I made n=10000, then you might not even see the dots in the corners, or see the line down the middle as actually a pair of bands just above and below the main diagonal.)
n = 50;
M3 = spdiags(ones(n,4),[-n+1, -1, 1, n-1],n,n);
spy(M3)
whos M3
Name Size Bytes Class Attributes M3 50x50 2008 double sparse
As you can see, M3 is a sparse version of the matrix you want to build.

추가 답변 (3개)

Bruno Luong
Bruno Luong 2023년 1월 22일
n = 8;
ismember(mod((1:n)-(1:n)',n),[1 n-1])
ans = 8×8 logical array
0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0

Sargondjani
Sargondjani 2023년 1월 22일
You can do something like:
A=[zeros(1,10);eye(9),zeros(9,1)]+ ....
Try to figure out the details yourself...

Walter Roberson
Walter Roberson 2023년 1월 22일
편집: Walter Roberson 2023년 1월 22일
circulant matrix
Or you could add two diagonal matrices
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 1월 22일
n = 8;
circshift(diag(ones(1,n)),1) + circshift(diag(ones(1,n)),-1)
ans = 8×8
0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0

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

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by