How to select non-sequential values in an array?

Illustration
Quincy - 2020-11-04T10:18:08+00:00
Question: How to select non-sequential values in an array?

I am trying to select the first, fifth, and ninth values of a 12x1 matrix. I am aware of how to reference sequential values like a(1:5,1) for the first five values but cannot figure out how to properly reference the non-sequential values. 

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

In MATLAB, you can select non-sequential values from an array using indexing. First, define your array and then create an index array with the positions of the values you want to select. Use this index array to extract the desired values. For example:

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
index = [1, 3, 5, 7, 9];  % Non-sequential indices
selected_values = array(index);

This way, you can easily retrieve non-sequential elements from your array.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!