Create a double identity matrix matlab

2 visualizzazioni (ultimi 30 giorni)
Afluo Raoual il 11 Mar 2022
Modificato: John D'Errico il 11 Mar 2022
If we have an identity matrix of dimensions (M*M) we use:
M=12;
K=eye(M);
But how can we obtain this matrix in general way: (it means double the identity)
K =
1 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 1 1
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposta accettata

John D'Errico il 11 Mar 2022
Modificato: John D'Errico il 11 Mar 2022
There are many basic ways, some not so basic. Perhaps my favorite is just:
n = 8;
A = triu(tril(ones(n)),-1)
A = 8×8
1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1
Here is another easy one:
A = eye(n) + diag(ones(n-1,1),-1)
A = 8×8
1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1
And yet another cute one is:
A = toeplitz([1 1,zeros(1,n-2)],[1,zeros(1,n-1)])
A = 8×8
1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1
Oh, wait. This next one is just too pretty to disregard:
flip(hankel([zeros(1,n-2),1 1]))
ans = 8×8
1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1
Of course, if n is seriously large, then you would want to create the result as a sparse matrix. Now spdiags is clearly a great solution.
n = 30;
A = spdiags(ones(n,2),[-1,0],n,n);
spy(A)
Actually, 30 is not at all large in context, but I made n only 30 so you could see that it worked. For a sparse matrix to be truly valuable, I'd only worry about it if n was on the order of 1000 or more.
The trick to solving such a problem in MATLAB is to get used to understanding how the various matrix tools can be used, and some experience in seeing how elements are stored in matrices. That helps you to see the many possible solutions to such a problem. Already in my mind I can see at least one other fairly simple solution. No, two more, at least. I could use meshgrid, maybe use indexing, or god forbid, a loop. Nah, skip the loop. Other ways should be there too.
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Più risposte (3)

KSSV il 11 Mar 2022
M=12;
K=eye(M);
K(2:1+size(K,1):end) = 1
K = 12×12
1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Ive J il 11 Mar 2022
Maybe not the best way, but works:
% taken from doc kron
n = 12;
I = speye(n, n);
E = sparse(2:n, 1:n-1, 1, n, n);
K = full(I + E)
K = 12×12
1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Sugar Daddy il 11 Mar 2022
M = 12;
K = transpose(eye(M)+triu(circshift(eye(M),1,2)))
K = 12×12
1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by