anonuser0428
anonuser0428

Reputation: 12343

Get values of elements at specific indices in a matlab matrix

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.

Upvotes: 3

Views: 10949

Answers (1)

anonuser0428
anonuser0428

Reputation: 12343

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

Upvotes: 3

Related Questions