Azzera filtri
Azzera filtri

How can I get the last numbers of an array from a for loop?

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Image Analyst
Image Analyst il 4 Lug 2018
Modificato: Image Analyst il 4 Lug 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.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by