Effacer les filtres
Effacer les filtres

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)
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
];

Réponse acceptée

John D'Errico
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])
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.

Plus de réponses (3)

Bruno Luong
Bruno Luong le 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 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...

Walter Roberson
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
Walter Roberson le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Sparse 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!

Translated by