4.10: Special 2D Arrays

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    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.10: Special 2D Arrays

    MATLAB includes built-in functions that create common matrices quickly. These are very useful when you need to initialize data.

    Function

    Description

    Example

    zeros(n)

    Creates an n-by-n matrix of zeros

    zeros(4)

    zeros(m,n)

    Creates an m-by-n matrix of zeros

    zeros(2,4)

    ones(n)

    Creates an n-by-n matrix of ones

    ones(4)

    ones(m,n)

    Creates an m-by-n matrix of ones

    ones(2,4)

    eye(n)

    Creates an n-by-n identity matrix

    eye(3)

    eye(m,n)

    Creates an m-by-n identity-style matrix

    eye(2,3)

    Example \(\PageIndex{1}\)

    Building a few special arrays.

    mat_1 = zeros(4)
    
    mat_2 =zeros(2,4)
    
    mat_3 =ones(4)
    
    mat_4 =ones(2,4)
    
    mat_5 =eye(3)
    
    mat_6 =eye(2,3)

    Solution

    mat_1 =
    
       0   0   0   0
       0   0   0   0
       0   0   0   0
       0   0   0   0
    
    mat_2 =
    
       0   0   0   0
       0   0   0   0
    
    mat_3 =
    
       1   1   1   1
       1   1   1   1
       1   1   1   1
       1   1   1   1
    
    mat_4 =
    
       1   1   1   1
       1   1   1   1
    
    mat_5 =
    
    Diagonal Matrix
    
       1   0   0
       0   1   0
       0   0   1
    
    mat_6 =
    
    Diagonal Matrix
    
       1   0   0
       0   1   0

    This page titled 4.10: Special 2D Arrays was last modified on Tue, 30 Jun 2026 02:11:57 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.