3.9: Scalar Arithmetic with Arrays

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.9: Scalar Arithmetic with Arrays

    One of the reasons MATLAB is so useful for engineering calculations is that it can perform the same operation on an entire array at once. This is different from doing calculations one value at a time.

    A scalar is a single number. For example, 3, -5, and 12.7 are scalars. When we perform an operation between a scalar and an array, MATLAB applies that operation to every element of the array.

    For example, suppose we create the array:

    myArray = [3 5 10 12];

    If we multiply this array by 3:

    myArray * 3

    MATLAB multiplies every element by 3 and returns:

    ans = [9 15 30 36]

    The operation is applied element by element automatically because one value is a scalar.

    The same idea works with addition, subtraction, and division. This is very convenient because we do not need to write separate commands for each element. MATLAB does the repeated calculation for us.

    For example, suppose an array stores temperatures in degrees Fahrenheit:

    tempF = [32 68 77 95];

    To convert all of these values to degrees Celsius, we can write:

    tempC = (5/9) * (tempF - 32)

    MATLAB subtracts 32 from every element of tempF, then multiplies every result by 5/9.

    The result is:

    tempC = [0 20 25 35]

    This is one of the first examples where we can see the real power of arrays: one line of code can process an entire list of values.

    Important Note

    Scalar operations work when one of the values is a single number and the other is an array. MATLAB applies the scalar to every element of the array.

    Example \(\PageIndex{1}\)

    Scalar addition

    myArray = [2 4 6]
    
    newArray = myArray + 10

    Solution

    myArray =
    
    2 4 6
    
    
    newArray =
    
    12 14 16

    This page titled 3.9: Scalar Arithmetic with Arrays 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.