Writing a loop to create a matrix

1 view (last 30 days)
if N==1
Lambda = A;
elseif N==2
Lambda = [A A^2]';
elseif N==3
Lambda = [A A^2 A^3]';
elseif N==4
Lambda = [A A^2 A^3 A^4]';
elseif N==5
Lambda = [A A^2 A^3 A^4 A^5]';
end
Given that A is a matrix, how do I create a loop that creates:
Lambda = [A A^2 A^3... A^(N-2) A^(N-1) A^N]';

Accepted Answer

Davide Masiello
Davide Masiello on 2 May 2022
Edited: Davide Masiello on 2 May 2022
Example
A = rand(3)
A = 3×3
0.5684 0.3903 0.6124 0.2054 0.1345 0.8194 0.4885 0.8078 0.9672
n = 10;
lambda = [];
for i = 1:n
lambda = [lambda;A.^i];
end
lambda
lambda = 30×3
0.5684 0.3903 0.6124 0.2054 0.1345 0.8194 0.4885 0.8078 0.9672 0.3231 0.1523 0.3750 0.0422 0.0181 0.6714 0.2386 0.6525 0.9355 0.1837 0.0594 0.2296 0.0087 0.0024 0.5502 0.1166 0.5271 0.9048 0.1044 0.0232 0.1406

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!