What is the best vectorized way to construct the following matrix
1 次查看(过去 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!