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

6 views (last 30 days)
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
];

Accepted Answer

John D'Errico
John D'Errico on 22 Jan 2023
Edited: John D'Errico on 22 Jan 2023
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.

More Answers (3)

Bruno Luong
Bruno Luong on 22 Jan 2023
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 on 22 Jan 2023
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 on 22 Jan 2023
Edited: Walter Roberson on 22 Jan 2023
circulant matrix
Or you could add two diagonal matrices
  1 Comment
Walter Roberson
Walter Roberson on 22 Jan 2023
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

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!