how can I write this in a compact form? can anyone suggest a single line code for it
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
SAHIL SAHOO
am 22 Jan. 2023
Beantwortet: Bruno Luong
am 22 Jan. 2023
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
];
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 22 Jan. 2023
Bearbeitet: John D'Errico
am 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])
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])
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
As you can see, M3 is a sparse version of the matrix you want to build.
0 Kommentare
Weitere Antworten (3)
Sargondjani
am 22 Jan. 2023
You can do something like:
A=[zeros(1,10);eye(9),zeros(9,1)]+ ....
Try to figure out the details yourself...
0 Kommentare
Walter Roberson
am 22 Jan. 2023
Bearbeitet: Walter Roberson
am 22 Jan. 2023
circulant matrix
Or you could add two diagonal matrices
1 Kommentar
Walter Roberson
am 22 Jan. 2023
n = 8;
circshift(diag(ones(1,n)),1) + circshift(diag(ones(1,n)),-1)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!