Fill a matrix with matrix powers
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Luigi Emanuel di Grazia
le 22 Juil 2021
Réponse apportée : Steven Lord
le 22 Juil 2021
Hi to everyone,
I was wondering if anyone knows the fastest way to achieve the following:
Given A [n x n], fill a matrix B such as:
B= [A 0n ... 0n;
0n A^2 ... 0n;
.... ;
0n 0n ... A^n]
where 0n=zeros(n).
Thanks in advance
0 commentaires
Réponse acceptée
Rik
le 22 Juil 2021
n=3;
A=rand(n,n);
Zero=zeros(size(A));
C=repmat({Zero},n,n);
C(logical(eye(n)))=arrayfun(@(n)A^n,1:n,'uni',false);
C=cell2mat(C)
0 commentaires
Plus de réponses (1)
Steven Lord
le 22 Juil 2021
A = magic(3);
AM = {A^0, A^1, A^2};
celldisp(AM)
B = blkdiag(AM{:})
You could create AM automatically rather than hard-coding it if you wanted a larger B.
AM2 = arrayfun(@(x) A^x, 0:2, 'UniformOutput', false);
check = isequal(AM, AM2)
C = blkdiag(AM2{:});
isequal(B, C)
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion 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!