How to Subset an Array in MATLAB

Written by chris daniels
  • Share
  • Tweet
  • Share
  • Email

MATLAB is a technical software package for manipulating and modelling large quantities of scientific or engineering data. In most cases, these data are stored in large, multidimensional arrays or matrices. MATLAB contains a variety of methods to extract subsets of the multidimensional arrays containing your data so you can conduct specific analyses.

Skill level:
Moderately Easy


  1. 1

    Construct your data array. This can be performed by importing your data for analysis. For an example, use the following code to generate a three-dimensional test array, composed of all zeros:

    test_array = zeros(256,256,50);

    In practice, this array could be a series of 50 256-by-256 images.

  2. 2

    Select a single data point by addressing that specific index in the array:

    my_data_point = test_array(128,128,10);

    The above code will return a single value from the pixel in the centre of the tenth image of the test array.

  3. 3

    Select a list of data points by addressing the list in the appropriate dimension:

    my_data_list = test_array([10 20 30],128,10);

    The above code will return a 3x1 vector of the 10th, 20th and 30th value of the 128th line in the tenth image of the test array.

  4. 4

    Select a range of data points by indexing the array using the start, increment and stop of the range each separated by a colon:

    my_data_range = test_array(10:5:100,128,10);

    The above code will return a 19x1 vector of the 10th, 15th, through the 100th values of the 128th line in the tenth image of the test array. Leaving the increment value off and only putting the start and stop value separated by a colon uses an increment of one.

  5. 5

    Select an entire dimension by indexing using a colon operator:

    my_data_line = test_array(:,128,10);

    The above code will return a 256x1 vector containing the 128th line of the 10th image in the test array.

Tips and warnings

  • When selecting subsets out of high-dimensional arrays, use the "squeeze" function to remove dimensions of length one, also called singleton dimensions.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.