How can I get the last numbers of an array from a for loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Carlos Nunez
am 4 Jul. 2018
Bearbeitet: Image Analyst
am 4 Jul. 2018
Let's say my for loop is giving me v= [1 2 3 4 5 6 7 8 9 10], rather than taking the whole array I only want it to be coming out v1= [1 2 ], v2=[3 4], v3= [5 6 ] and so on
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 4 Jul. 2018
Bearbeitet: Image Analyst
am 4 Jul. 2018
No for loop needed. This will do it:
v = [1 2 3 4 5 6 7 8 9 10]
v1 = v(1:2)
v2 = v(3:4)
v3 = v(5:6)
v4 = v(7:8)
v5 = v(9:10)
You should read the getting started section of the help to learn how to do array indexing.
If you have more than a few of these v's, I would not create uniquely named, separate variables but just leave it as an array, for reasons outlined in the FAQ https://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F I'm sure Stephen will also give you more reasons not to do this for lots of variables.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!