I want to build the matrix
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to build the matrix G from a square matrix A, which has n×n dimensions.
The matrix G is defined as :
G = [ I O O O ...;
A I O O ... ;
A^2 A I O ...;
A^3 A^2 A I ...]
where:
- I is the n×n identity matrix (I=eye(n)),
- O is the n×n zero matrix (O=zeros(n)).
The resulting G has dimensions (n⋅m)×(n⋅m), where m is the number of block rows and columns in G
Can any one Help me
Thanx
3 commentaires
Mr. Pavl M.
le 31 Déc 2024
- Good question and good answer of Stephen23 and M. Agarwal, I checked both are correct, first is with no explicit loops, second is with 1 loop, I tested the running times and in 2 IDEs (both in Mt-b and Oc-e it runs, who will require it in TCE Julia, in TCE Python?), the 2 constructing methods summarized: as well,why function runs faster?:
clc
clear all
close all
m = 4;
n = 3;
A = randi(9,n)
C = [{zeros(n),eye(n)},arrayfun(@(p)A^p,1:m-1,'uni',0)];
tic
t1 = cputime;
G1 = cell2mat(C(1+tril(toeplitz(1:m))))
toc
t2 = cputime;
display(t2-t1)
function G = constructG(A, m)
n = size(A, 1);
% Create block diagonal of identity matrices
G = kron(eye(m), eye(n));
% Fill lower triangular blocks with powers of A
current_power = A;
for i = 1:m-1
% Fill the i-th subdiagonal
for j = 1:m-i
row_idx = (i+j-1)*n + (1:n);
col_idx = (j-1)*n + (1:n);
G(row_idx, col_idx) = current_power;
end
if i < m - 1
current_power = current_power * A;
end
end
end
tic
t1 = cputime;
G2 = constructG(A, m)
toc
t2 = cputime;
display(t2-t1)
Réponse acceptée
Malay Agarwal
le 31 Déc 2024
Modifié(e) : Malay Agarwal
le 31 Déc 2024
You can build the required matrix using the following function:
function G = constructG(A, m)
n = size(A, 1);
% Create block diagonal of identity matrices
G = kron(eye(m), eye(n));
% Fill lower triangular blocks with powers of A
current_power = A;
for i = 1:m-1
% Fill the i-th subdiagonal
for j = 1:m-i
row_idx = (i+j-1)*n + (1:n);
col_idx = (j-1)*n + (1:n);
G(row_idx, col_idx) = current_power;
end
if i < m - 1
current_power = current_power * A;
end
end
end
You can use the function as follows:
A = randi(5, 3)
G = constructG(A, 4)
Here are some details on the implementation:
- The kron() function is used to create a
matrix where each
block diagonal is the identity matrix
.
- Then, the lower triangular
blocks are filled with the powers of A. The powers of A are calculated incrementally to make the function more efficient.
Refer to the following resources for more information:
- eye() function - https://www.mathworks.com/help/matlab/ref/eye.html
- kron() function - https://www.mathworks.com/help/matlab/ref/kron.html
Hope this helps!
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!