Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Image Analyst
Image Analyst el 4 de Jul. de 2018
Editada: Image Analyst el 4 de Jul. de 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.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by