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

1 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2018-7-4
编辑:Image Analyst 2018-7-4
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 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by