Create a double identity matrix matlab

2 views (last 30 days)
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

Accepted Answer

John D'Errico
John D'Errico on 11 Mar 2022
Edited: John D'Errico on 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.

More Answers (3)

KSSV
KSSV on 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

Ive J
Ive J on 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

Sugar Daddy
Sugar Daddy on 11 Mar 2022
Daddy is here for help
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

Tags

Community Treasure Hunt

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

Start Hunting!