don't know how to write my for loop
2 ビュー (過去 30 日間)
古いコメントを表示
I have a 21x21 matrix (A), I also have a vector V0=[1 zeros(1,20)]'.
I have an equation I want to base the for loop on: V_(n+1)=AV_n
I want to run the for loop between n=1:120
The results needs to be a 21x1 vectors.
I have done it the long way to understand what im looking for but I have no idea how to get it
V1 = A* V0
V2 = A* V1
V3 = A* V2...
V120 = A* V119
Im just struggling to figure out the for loop...ive given it a crack but still no luck. Any help would be much appreciated.
for n = 1:120
V(:,n+1) = A*V0(:,n);
end
0 件のコメント
回答 (2 件)
Antoni Garcia-Herreros
2023 年 3 月 20 日
Hello Kellie,
Something like this should do the trick
V=zeros(21,120);
V(:,1)=A*V0; %This is your V1
for n = 1:119
V(:,n+1) = A*V(:,n);
end
Hope this helps!
Stephen23
2023 年 3 月 20 日
編集済み: Stephen23
2023 年 3 月 20 日
The loop might be clearer iterating from 2, for example:
N = 121; % total number of vectors
A = rand(21,21);
M = nan(21,N);
M(:,1) = 0;
M(1,1) = 1;
for k = 2:N
M(:,k) = A*M(:,k-1);
end
Checking the output (each column of M is one vector):
format longG
display(M)
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!