how to index a vector by another one?

idx=[-2,-1,0,1,2,3];
x=[6,5,2,1,8,0];
is there a way to get that x(-2)=6,...,x(3)=0

Answers (1)

MATLAB indexing begins with 1 and includes all non-negative integers.
You have to do something like this:
idx=[-2,-1,0,1,2,3];
x=[6,5,2,1,8,0];
Result = x(idx+abs(min(idx))+1);

1 Comment

Why the abs? I am not sure that it helps. Perhaps:
Result = x(idx+1-min(idx))

This question is closed.

Tags

Asked:

on 11 May 2017

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!