필터 지우기
필터 지우기

Indexing problem based on value?

조회 수: 1 (최근 30일)
HYZ
HYZ 2020년 5월 22일
편집: SHIVANI CHORGE 2020년 5월 22일
Hi,
I encountered a strange problem. For A= [5 4 3 2 1 0 1 2 3 4 5];
I can't do B = A(1): A(6). The return is 1×0 empty double row vector. if I do A(7): A(8) or start from index 7, I can get the output.
I am not sure what is the source of problem. I expected A(1) : A(6) = [5 4 3 2 1 0].
I tried A(1:6) and it works. what is the difference between A(1:6) and A(1): A(6) while the latter doesn't work?
Thanks.

채택된 답변

KSSV
KSSV 2020년 5월 22일
편집: KSSV 2020년 5월 22일
B = A(1:6)
You should read about array indexing in MATLAB. MATLAB indices are always nonzero positive integers.
When you use A(1:6), it means in the array A pick indices from 1 to 6.
When you try A(1):A(6), it will try to generate an array with a difference of 1, from the value A(1) i.e 5 to A(6) i.e 0, this will work when start value is low and end value is high, in your case they are 5 and 0, so :, cannot generate an array. Where as try A(6):A(1), it will work.

추가 답변 (1개)

SHIVANI CHORGE
SHIVANI CHORGE 2020년 5월 22일
편집: SHIVANI CHORGE 2020년 5월 22일
A(1) : A(6)=A(1:6)
A= [5 4 3 2 1 0 1 2 3 4 5]
this is in vector form hence A(1) : A(6) this is not working
if you expected like this A(1) : A(6) = [5 4 3 2 1 0]
than use it
A(1) : A(6)=A(1:6)

카테고리

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