# don't know how to write my for loop

2 views (last 30 days)
Kellie on 20 Mar 2023
Commented: Kellie on 20 Mar 2023
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

Antoni Garcia-Herreros on 20 Mar 2023
Hello Kellie,
Something like this should do the trick
V=zeros(21,120);
for n = 1:119
V(:,n+1) = A*V(:,n);
end
Hope this helps!
Kellie on 20 Mar 2023
Yes it did thank you Antoni! oh I would never have thouyght of that, Im new to Matlab code so thank you!

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)
M = 21×121
1.0e+00 * 1 0.756030195816377 6.41073378156875 61.8054737412763 645.929151064815 6717.5627693989 69976.8958361933 728860.563737083 7591647.01531391 79072804.583534 823603671.315581 8578461264.46048 89351225876.9822 930661259503.826 9693547810039.16 100965703886543 1.0516349185118e+15 1.09535808622318e+16 1.14089910475051e+17 1.18833355374099e+18 1.23774015516954e+19 1.28920090398545e+20 1.34280120419074e+21 1.39863001057627e+22 1.45677997635063e+23 1.51734760690694e+24 1.58043341998275e+25 1.64614211247876e+26 1.71458273421325e+27 1.78586886890067e+28 0 0.345678117788818 4.3801420765591 47.7227020458373 501.261008654915 5226.0838658496 54431.1700291016 566941.07402669 5905121.0421751 61506334.2058719 640635440.873299 6672707387.4008 69501343722.1761 723909576472.794 7540071124769.88 78535599492035.5 818008250255971 8.52018068003202e+15 8.87441890686019e+16 9.24338507503827e+17 9.6276915189785e+18 1.00279760317382e+20 1.04449029234981e+21 1.08791641240479e+22 1.13314803311099e+23 1.18026022064051e+24 1.22933116214476e+25 1.28044229551349e+26 1.33367844452867e+27 1.38912795963766e+28 0 0.865052097768293 3.64127129633876 40.5687627053758 419.510007047771 4374.71937780249 45562.8013368784 474578.478439059 4943094.34523597 51486109.7197482 536267153.802015 5585631985.33998 58178623159.9979 605974794115.577 6311690293857.69 65741074962524 684743505465912 7.13212658212486e+15 7.42865455128953e+16 7.73751107849146e+17 8.05920874048255e+18 8.39428142509794e+19 8.74328521729358e+20 9.10679932202207e+21 9.48542702547833e+22 9.87979669631032e+23 1.02905628284565e+25 1.07184071273409e+26 1.11640396412275e+27 1.16281999396137e+28 0 0.265810357913987 4.32601804370417 48.1376198226492 500.0025014813 5205.77159946657 54217.2077543022 564712.198369412 5881903.39820092 61264517.6962136 638116742.001411 6646473233.97879 69228094967.2003 721063482031.884 7510426877410.43 78226832015062.2 814792201162100 8.48668307246272e+15 8.83952859019772e+16 9.20704414548717e+17 9.58983966531336e+18 9.9885504352115e+19 1.04038381535865e+21 1.08363920298641e+22 1.12869299282997e+23 1.17561995593423e+24 1.22449797205306e+25 1.27540815889819e+26 1.32843500676177e+27 1.38366651873599e+28 0 0.242053896735137 4.54237932930235 48.8280887890181 507.56236103176 5291.92826817291 55119.5647126416 574109.033920883 5979783.38796313 62284010.8454343 648735533.094914 6757076053.26113 70380107806.893 733062575614.376 7635406601387.71 79528591293562.2 828351018266031 8.62790850814372e+15 8.98662566754893e+16 9.36025698608441e+17 9.74942254042262e+18 1.01547681877763e+20 1.05769666377598e+21 1.10167185688147e+22 1.14747537910521e+23 1.19518324574421e+24 1.24487463253597e+25 1.29663200705813e+26 1.35054126559127e+27 1.40669187567194e+28 0 0.342109287742779 5.53033677455528 59.1974977314086 619.737584968891 6453.86423995563 67223.0743540745 700176.778325205 7292868.45911468 75960801.7826828 791189745.551739 8240845499.24821 85834699769.1768 894033954403.453 9312046453225.45 96992075883874.8 1.01024654800634e+15 1.0522489373059e+16 1.09599763369289e+17 1.14156524228564e+18 1.18902738686007e+19 1.23846283535455e+20 1.28995363059282e+21 1.34358522644179e+22 1.39944662963042e+23 1.45763054746479e+24 1.51823354168469e+25 1.58135618871703e+26 1.64710324659195e+27 1.71558382879873e+28 0 0.834917844906488 6.54375069853436 67.0740823933321 691.983814327904 7207.75908930214 75067.8922860826 781897.093417651 8144048.63778624 84826497.5251635 883532755.074543 9202668449.62991 95852820438.8635 998380332440.448 10398893675138.7 108312419779235 1.12815657558693e+15 1.17506123640673e+16 1.22391602299298e+17 1.27480201450577e+18 1.3278036610828e+19 1.38300892399234e+20 1.44050942161332e+21 1.50040058148479e+22 1.5627817986769e+23 1.62775660074653e+24 1.6954328195511e+25 1.76592277020573e+26 1.83934343748092e+27 1.91581666994984e+28 0 0.43437012507869 4.79351805417269 44.5685086653659 467.92208472646 4863.08056885352 50661.5394235947 527667.408855102 5496079.63296448 57245847.3288371 596259251.225658 6210495465.17068 64687053563.0724 673765067535.49 7017777766332.32 73095515262483.3 761345618142042 7.9299960904931e+15 8.2596965815207e+16 8.6031048237935e+17 8.96079073591736e+18 9.33334793164781e+19 9.72139470504848e+20 1.01255750566088e+22 1.05465597620241e+23 1.09850474854115e+24 1.14417659388095e+25 1.1917473089885e+26 1.2412958419853e+27 1.29290442336956e+28 0 0.979495238304799 5.67072565212731 61.4070876927154 630.791275109228 6575.52134574229 68479.5695016467 713269.759993487 7429245.97831048 77381276.9461956 805985104.948047 8394950437.30941 87439820476.8476 910752512367.031 9486183003404.41 98805841050455.2 1.02913829747944e+15 1.07192613723901e+16 1.11649294026893e+17 1.16291266941316e+18 1.21126236262265e+19 1.26162226080705e+20 1.31407594100219e+21 1.36871045507407e+22 1.4256164741897e+23 1.48488843929456e+24 1.54662471784627e+25 1.61092776706475e+26 1.67790430396973e+27 1.74766548248776e+28 0 0.715740047525819 6.21812072601196 61.3025738209505 633.121692017142 6593.20820706366 68669.3682437419 715254.052055525 7449917.57036225 77596604.1847649 808227885.326676 8418310745.03796 87683135846.2468 913286827867.531 9512579834752.79 99080784209756.8 1.032002040471e+15 1.07490894428307e+16 1.11959976161714e+17 1.16614866113073e+18 1.21463289514529e+19 1.26513292785373e+20 1.31773256885842e+21 1.3725191122612e+22 1.42958348153621e+23 1.48902038042605e+24 1.55092845011162e+25 1.61541043291657e+26 1.6825733428179e+27 1.75252864304587e+28

### Categories

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

R2022b

### Community Treasure Hunt

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

Start Hunting!