3
votes

I was wondering if there is a quick way in matlab (pretty sure there is and I just don't know it!) to get all the values of an array at specific indices. Example: If I have an array:

a = [1,3,5,7,9]

and I have my list of indices [2,3], I am looking for a function that will give me back [3,5] which are the values at indices 2,3 in the array 'a'. The straightforward solution is to loop through and index one at a time but I would like to know of a faster more efficient built-in function if one exists.

And as far as I have read, find function only returns the index where a particular value is present. I am looking for the opposite scenario wherein I pass in the indices as a array and all the values present in 'a' at those indices are returned. Any help would be much appreciated!

Thanks in advance.

1
Matrix indexing is basic MATLAB. Hard to do anything without it...chappjc

1 Answers

3
votes

Found it! It's as simple as doing a([2,3]) and the required values are returned!