필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Carlos Nunez
Carlos Nunez 2018년 7월 4일
편집: Image Analyst 2018년 7월 4일
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