How can I index multiple, nonsequential, nonlinearly spaced values?

92 views (last 30 days)
How would I go about indexing the first, sixth, and seventh numbers in an array all with one command?

Answers (2)

Star Strider
Star Strider on 21 Jan 2016
Use a vector of indices to address them in any order you want:
V = randi(9, 1, 10);
R1 = V([1 5 9]);
R2 = V([6 2 8]);
The same goes for matrices. You ideally should have a separate vector of indices for each array dimension, but since matrices are also addressed linearly, you can also address them using the ind2sub function and its friends. See the documentaiton on Matrices and Arrays for more details.

Linna He
Linna He on 19 Feb 2020
array([1 6 7])

Products

Community Treasure Hunt

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

Start Hunting!