Writing a loop to create a matrix

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]';

 Risposta accettata

Davide Masiello
Davide Masiello il 2 Mag 2022
Modificato: Davide Masiello il 2 Mag 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by