a simple vector to a matrix

Hello everyone,
I am trying to write a code that convert a vector like the following for example:
X=[1 2 3];
to a matrix like the following:
Z=[1 0 0 2 0 0 3 0 0;...
0 1 0 0 2 0 0 3 0;...
0 0 1 0 0 2 0 0 3];
Any ideas?
Thanks very much.

답변 (2개)

Steven Lord
Steven Lord 2021년 10월 6일

2 개 추천

X=[1 2 3];
I = eye(3);
kron(X, I)
ans = 3×9
1 0 0 2 0 0 3 0 0 0 1 0 0 2 0 0 3 0 0 0 1 0 0 2 0 0 3
kron(I, X)
ans = 3×9
1 2 3 0 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3
David Hill
David Hill 2021년 10월 6일

0 개 추천

X-[1 2 3];
Z=zeros(3,3*numel(X));
for k=1:numel(X)
Z(9*k:-4:9*k-8)=X(k);
end

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

MA
2021년 10월 6일

답변:

2021년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by