I want to make an identity matrix

조회 수: 8 (최근 30일)
Qonitat
Qonitat 2023년 3월 5일
편집: John D'Errico 2023년 3월 5일
i want to make a matrix that look like this
How could i achieve this efficiently .Thanks in advace

답변 (2개)

the cyclist
the cyclist 2023년 3월 5일
I've made some assumptions about the pattern, but I expect it is what you want.
This should be highly efficient. The algorithm is a bit obfuscated.
% Input
N = 4;
% Start with all 0s
M = zeros(N,N*(N+1));
% Fill in the 1s
M(1:N*N+1:end-N*N) = 1;
% Fill in the -1s
M(:,N*N+1) = -1;
% Display the result
disp(M)
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 0 0 0

John D'Errico
John D'Errico 2023년 3월 5일
편집: John D'Errico 2023년 3월 5일
These are starting to look like homework questions.
Did you not read the answer to the last question you asked? You could not use that idea to build this matrix?
A = kron([eye(4),zeros(4,1)],[1 0 0 0])
A = 4×20
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
That gets you almost there, with the first 16 columns correct, and a total of 20 columns. Then it would be simple enough to stuff in the -1 elements in column 17.
A(:,17) = -1
A = 4×20
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 0 0 0
spy(A)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by