What is the best vectorized way to construct the following matrix

1 回表示 (過去 30 日間)
Mohamed Abdalmoaty
Mohamed Abdalmoaty 2015 年 3 月 8 日
回答済み: Guillaume 2015 年 3 月 8 日
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)

採用された回答

Guillaume
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{:})

その他の回答 (1 件)

rantunes
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

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by