How to Matrix Multiply instead of a for loop?
Show older comments
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
Answers (2)
KSSV
on 29 Apr 2019
A = magic(4) ;
n = 1:size(A,2) ;
iwant = A.^n
Guillaume
on 29 Apr 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
Categories
Find more on Loops and Conditional Statements 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!