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

 採用された回答

Davide Masiello
Davide Masiello 2022 年 5 月 2 日
編集済み: Davide Masiello 2022 年 5 月 2 日

1 投票

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2020b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by