4.4: Extracting Rows, Columns, and Subarrays

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    4.4: Extracting Rows, Columns, and Subarrays

    Once a matrix is created, we often do not want the entire matrix. We might want one row, one column, or a smaller section. MATLAB makes this easy with indexing and the colon operator.

    Example \(\PageIndex{1}\)

    Extracting a block of data.

    mat2 = [2 10 5; 6 20 17; 13 2 0];
    
    data_1 = mat2(1,:)      % entire first row
    
    data_2= mat2(:,3)      % entire third column
    
    data_3 = mat2([2,3],[1,2])  % rows 2 and 3, columns 1 and 2
    
    Solution

    The colon symbol means "all" in this context. So mat2(1,:) means row 1 and all columns. Similarly, mat2(:,3) means all rows and column 3.

    data_1 =
    
        2   10    5
    
    data_2 =
    
        5
       17
        0
    
    data_3 =
    
        6   20
       13    2

    This page titled 4.4: Extracting Rows, Columns, and Subarrays was last modified on Tue, 30 Jun 2026 02:11:56 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.