Creating a Matrix from a given Vector.

Hello, I want to make a matrix with the next features, given a vector, the columns of the matrix must be: In the first column, the vector and the other elements of the column zero, then for the next column, 0 followed by the vector and then the others elements zeros, and so on, on the last column, the final elements of it must ve the vector, here is an example:
h=[1, -0.5]
The matrix must be:
[1 , 0 , 0; -0.5, 1 , 0; 0 , -0.5, 1; 0 ,0 , -0.5]

답변 (2개)

Ahmet Cecen
Ahmet Cecen 2016년 5월 22일

0 개 추천

Check "diag" function.
Andrei Bobrov
Andrei Bobrov 2016년 5월 22일
편집: Andrei Bobrov 2016년 5월 23일

0 개 추천

h=[1, -0.5];
m = 3; % m - number of columns in out - matrix
out = full(spdiags(ones(m,1)*fliplr(h(:)'),[-1 0],m + numel(h) - 1,m));
or
out = toeplitz([h(:);zeros(m-1,1)],[h(1);zeros(m-1,1)]);
or
n = numel(h);
m1 = m + n - 1;
out = zeros(m1,m);
out(bsxfun(@plus,(1:n)',(0:m-1)*(m1+1))) = 1;
out(out>0) = h(:)*ones(1,m);

댓글 수: 4

The answer is a 4*4 matrix but should be a 4*3 matrix.
Andrei Bobrov
Andrei Bobrov 2016년 5월 22일
편집: Andrei Bobrov 2016년 5월 22일
Where is 4*4 in answer?
Carlos Martínez
Carlos Martínez 2016년 5월 23일
편집: Carlos Martínez 2016년 5월 23일
Sorry, I didn't realize you edited, but, ¿for a 1xn vector?
Andrei Bobrov
Andrei Bobrov 2016년 5월 23일
Corrected. Work for any vectors.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

질문:

2016년 5월 22일

편집:

2016년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by