Create a double identity matrix matlab
显示 更早的评论
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
采纳的回答
更多回答(3 个)
M=12;
K=eye(M);
K(2:1+size(K,1):end) = 1
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)
Daddy is here for help
M = 12;
K = transpose(eye(M)+triu(circshift(eye(M),1,2)))
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
