What is the best vectorized way to construct the following matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a system of linear equations:
y = A x
y is a column vector of size n. A is a lower triangular matrix of size n by n. x is a column vector of size n.
I want to construct the following block diagonal matrix
the first block is [y(1) A(1,1); x(1) 1;]
which is just 2 by 2 matrix
the second block is [y(2) A(2,1) A(2,2); x(1) 1 0; x(2) 0 1;];
which is 3 by 3 matrix
the kth block should look like [y(k) A(k,1) ... A(k,k); x(1:k) eye(k);];
which is a k+1 by k+1 matrix.
If it will help, we can assume that A is a lower triangular toeplitz matrix This means that the diagonal of A is constant = A(1,1) The 1st sub-diagonal is constant and = A(2,1)
댓글 수: 0
채택된 답변
Guillaume
2015년 3월 8일
I would do it like this:
x = randi(20, 5, 1); %demo data
A = tril(toeplitz(randi(20,size(x)))); %demo data
y = A * x; %demo data
blocks = arrayfun(@(k) [y(k) A(k, 1:k); x(1:k) eye(k)], 1:numel(x), 'UniformOutput', false);
blockmat = blkdiag(blocks{:})
댓글 수: 0
추가 답변 (1개)
rantunes
2015년 3월 8일
Hey,
In a glance, it seems that the best way to implement is to do it block by block.
B = zeros(k+1,k+1);
for i = 1:k+1
if i == 1
B(i,1) = y(k);
end
B(i,1) = x(i-1);
end
for i = 1:k
B(1,i+1) = A(k,i);
end
for i = 2:k+1
for j = 2:k+1
if i == j
B(i,j) = 1;
end
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!