Negative indexing of an array

조회 수: 218 (최근 30일)
Naveen Kumar Samannan
Naveen Kumar Samannan 2020년 9월 10일
답변: Walter Roberson 2020년 9월 10일
I have an array say x= [1 2 3 4 5 6] which means that x(1)=1 ,x(2)=2 and so on . I just what to shift this array in such a way that x(-3)=1 ,x(-2)=2, x(-1)=3 ....and so on . What to do??? Please help....

답변 (2개)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 9월 10일
There is no non-positive indexing in Matlab.
you can use x(end) for the last member, x(end-1) the member before the last member and so on.

Walter Roberson
Walter Roberson 2020년 9월 10일
x = @(idx) x(idx+4)
This does appear to use the same "x" on the left side and the right side, but it does not really do so. At the time the anonymous function is created, the current value of the matrix x will be copied into the anonymous function handle, and afterwards the anonymous function will retrieve values from that copy.
This means that you can only use the above in expressions, not as the destination of an assignment. For example,
y = x(-2) %would work
x(-2) = 7 %would fail

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by