4.5: Modifying and Extending a Matrix

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.5: Modifying and Extending a Matrix

    You can change one element of a matrix by assigning a new value to that row and column location.

    Example \(\PageIndex{1}\)

    Changing one element.

    mat2 = [2 10 5; 6 20 17; 13 2 0];
    mat2(2,2) = 100
    Solution

    This changes the value in row 2, column 2 from 20 to 100.

    mat2 =
    
         2    10     5
         6   100    17
        13     2     0

    Changing an Entire Row or Column

    You can also replace an entire row or column. The replacement must have the correct number of values.

    mat2(1,:) = 200:202 % replace the entire first row

    Since the first row has three entries, the replacement row must also have three entries: 200, 201, and 202.

    Caution

    Watch the dimensions: If you replace an entire row with a new row, the new row must have the same number of columns. If you replace an entire column, the new column must have the same number of rows.

    Extending a Matrix

    MATLAB also allows you to extend a matrix by adding a new row or column. For example, we can add a fourth row to a 3-by-3 matrix.

    Example \(\PageIndex{1}\)
    clear;
    mat2 = [2 10 5; 6 20 17; 13 2 0]
    mat2(4,:) = [9 18 27]

    Solution

    This creates a new fourth row. If you skip rows or columns while extending an array, MATLAB fills the missing entries with zeros.

    mat2 =
    
    2 10 5
    6 20 17
    13 2 0
    9 18 27

    This page titled 4.5: Modifying and Extending a Matrix 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.