Create a matrix with same diagonal elements

조회 수: 5 (최근 30일)
Grzegorz Lippe
Grzegorz Lippe 2013년 11월 27일
댓글: Grzegorz Lippe 2013년 11월 27일
Hello World,
I have a vector
x = [x_1 x_2 x_3]
and would like to create a Matrix which looks like this:
X = [x_1 0 0
x_2 x_1 0
x_3 x_2 x_1]
Is there a loop-less way or a Matlab function to do this? The best thing I could come up with is:
x = 1:3 ;
N = length(x) ;
X = zeros(N) ;
for kk=1:N
X(kk:N+1:end) = x(kk) ;
end
X = tril(X)

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 27일
편집: Azzi Abdelmalek 2013년 11월 27일
X=[10 20 30]
n=numel(X);
out=tril(cell2mat(arrayfun(@(x) circshift(X',[x 0]),0:n-1,'un',0)))

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2013년 11월 27일
out = tril(toeplitz(x));

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by