how can I write this in a compact form? can anyone suggest a single line code for it
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
SAHIL SAHOO
le 22 Jan 2023
Réponse apportée : Bruno Luong
le 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 commentaires
Réponse acceptée
John D'Errico
le 22 Jan 2023
Modifié(e) : John D'Errico
le 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 commentaires
Plus de réponses (3)
Sargondjani
le 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 commentaires
Walter Roberson
le 22 Jan 2023
Modifié(e) : Walter Roberson
le 22 Jan 2023
circulant matrix
Or you could add two diagonal matrices
1 commentaire
Walter Roberson
le 22 Jan 2023
n = 8;
circshift(diag(ones(1,n)),1) + circshift(diag(ones(1,n)),-1)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!