3.3: Creating One-Dimensional Numeric Arrays (Vectors)

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.

    3.3: Creating One-Dimensional Numeric Arrays (Vectors)

    A vector can be a row vector, which has one row and several columns, or a column vector, which has several rows and one column.

    Creating Numeric Row Vectors

    There are several common ways to create a row vector in MATLAB. The best choice depends on the pattern of the numbers you want to store.

    Method 1: Type the Values Directly

    The simplest method is to type the values inside square brackets. Values in the same row can be separated by spaces or commas.

    array1 = [1 2 3 4]
    array2 = [1, 2, 3, 4]
    

    Both commands create the same row vector. Using spaces is common in MATLAB, but commas are also acceptable. Pick a style and be consistent.

    Method 2: Use the Colon Operator

    When the numbers are regularly spaced, the colon operator is usually faster and cleaner than typing every value.

    The general format is:

    startValue : stepSize : endValue

    If the step size is not written, MATLAB assumes a step size of 1. The colon operator always includes the starting value. It includes the ending value only if the sequence lands exactly on it.

    For example, 1:2:8 stops at 7 because the next value would be 9, which is past 8.

    Example \(\PageIndex{1}\)

    Using colon operator.

    array3 = 1:5        % 1 2 3 4 5
    array4 = 9:-2:1     % 9 7 5 3 1
    array5 = 1:2:8      % 1 3 5 7
    Solution
    array3 = 1 2 3 4 5
    
    array4 = 9 7 5 3 1
    
    array5 = 1 3 5 7
    

    Method 3: Use linspace

    The linspace function creates a linearly spaced vector with a specified first value, last value, and number of elements.

    The general format is:

    linspace(startValue, endValue, numberOfElements)

    Example \(\PageIndex{2}\)

    Creating an row vector using linspace.

    array6 = linspace(3, 15, 5)
    Solution
    array6 =
    
         3     6     9    12    15
    

    This creates five values beginning at 3 and ending at 15. MATLAB automatically calculates the spacing between the values.

    Exercise \(\PageIndex{1}\)

    Create a row vector that stores the even numbers from 2 through 20

    Answer

    Option 1: rowVector = 2:2:20

    Option 2: rowVector = linspace(2, 20, 10)

    Exercise \(\PageIndex{2}\)

    create a row vector with 6 equally spaced values from 0 to 1

    Answer

    rowVector2 = linspace(0, 1, 6)

    Creating Numeric Row Vectors

    A column vector has one column and multiple rows. You can create one directly by separating elements with semicolons, or you can transpose a row vector.

    For example:

    colVec1 = [1; 2; 3; 4]
    

    Will produce:

    colVec1 =
    
        1
        2
        3
        4

    The apostrophe symbol transposes an array. For example, a row array can be changed into a column array using the transpose operation.

    Example \(\PageIndex{3}\)

    Transposing a row vector to create a column vector.

    rowVec = 1:10
    colVec2 = rowVec'
    Solution
    rowVec =
    
         1     2     3     4     5     6     7     8     9    10
    
    
    colVec2 =
    
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
    

    This page titled 3.3: Creating One-Dimensional Numeric Arrays (Vectors) was last modified on Tue, 30 Jun 2026 02:11:55 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.