What is the best vectorized way to construct the following matrix
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Guillaume
on 8 Mar 2015
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 Comments
More Answers (1)
rantunes
on 8 Mar 2015
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 Comments
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!