don't know how to write my for loop
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Antoni Garcia-Herreros
on 20 Mar 2023
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
on 20 Mar 2023
Edited: Stephen23
on 20 Mar 2023
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 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!