How to Matrix Multiply instead of a for loop?
Afficher commentaires plus anciens
Say A is a square Matrix, and i want the end resut to be [A;A^2,A^3......A^n], this can be done with a for loop, is there any efficient algorithm to reduce the computation time?
thankyou
Réponses (2)
KSSV
le 29 Avr 2019
A = magic(4) ;
n = 1:size(A,2) ;
iwant = A.^n
Guillaume
le 29 Avr 2019
I can't see a way to do that without a loop. The following is fairly efficient:
A = rand(4) %demo matrix
n = 10; %demo data
h = size(A, 1);
result = [A; zeros(h*(n-1), size(A, 2))];
for row = h+1:h:h*n
result(row:row+h-1, :) = result(row-h:row-1, :) * A;
end
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!