CHAPTER 4

Arithmetic Operations on Elements in Arrays

4.1 Operations on Scalars
4.2 Operations on Vectors of the Same Length
4.3 Operations of a Vector with a Scalar
4.4 Matrices Used with the Operators


Many of the basic operators on arrays were demonstrated or at least used in the introductory material on MATLAB. We will try to include a more complete list of those operators that are primarily arithmetic here and to emphasize the way each treats different types of arrays. First we will look at the operators associated with one or two symbols as they perform on scalars. This Includes:

Symbol(s)

Performs the Operation on Scalars

+

Addition

-

Subtraction

*

Multiplication

/

First Divide: Left over right

\

Second Divide: Right over left

^

Raise to a Power

,

Catenates to form vectors when used with [ and ]

;

Catenates to form matrices when used with [ and ]

:

Used in setting the range for a vector





4.1 Operations on Scalars


First let's see how these perform with scalars. We will use the disp function to display our results. Here are some simple arithmetic operations:

>> disp(3+5) 
     8      <-- Three plus five is eight
 
>> disp(3-5) 
    -2      <-- Three minus five is negative 2
 
>> disp(3*5) 
    15     <-- Three times five is fifteen
 
>> disp(3/5) 
    0.6000  <-- Three divided by five is six tenths
 
>> disp(3\5) 
    1.6667  <-- Five divided by three is one and two thirds
 
>> disp(5^3) 
    125     <-- Five cubed is one hundred twenty five 
 


Note that blank lines are frequently erased from these notes. The second divide operator gives the second number over the first one. Raising a number to a power is done with the "^" symbol.

Now let's look at catenation: combining scalars. We use square brackets in MATLAB to catenate things together:

>> disp([3,5]) 
     3     5 
 
>> disp([3 5]) 
     3     5 

The comma is not even necessary. We can also do the same thing with character data but the way it prints is quite different:

>> disp(['a' 'b']) 
     ab 

What does the semicolon do?

>> disp([3;5]) 
     3 
     5 
 
>> disp(['a';'b']) 
     a 
     b 

It puts the elements in separate rows. There is alternate way to do that too:

>> [3 
    5] 
ans = 
     3 
     5  

The colon produces a vector from two or three scalars:

>> disp(1:3) 
     1     2     3 
 
>> disp(0.1:3) 
    0.1000    1.1000    2.1000 
 
>> disp(3:2) 
ans = 
     [] 

Two scalars (a and b) separated by a colon give a vector with elements:

a, a+1, a+2, ... a+n
where n is the largest integer such that a+n < b

Three scalars separated by colons give the same sort of vector, but with
a spacing between elements equal to the middle scalar as seen in the examples:

>> disp(1:.5:2) 
    1.0000    1.5000    2.0000 
 
>> disp(3:-1:1) 
     3     2     1 


4.2 Operations on Vectors of the Same Length


Now let's see what care must be exercised in using the operators on vectors and matrices. First the operators used with vectors of the same length will be shown where they make sense.

>> disp([1 2]+[-2 2]) 
    -1     4 
 
>> disp([1 2]-[-2 2]) 
     3     0 
 
>> disp([1 2]*[-2 2]) 
 
??? Error using ==> * 
Inner matrix dimensions must agree. 

Oops! That last one does not work on vectors. If we want to multiply elements together, we need to use a period to indicate that is our intention:

>> disp([1 2].*[-2 2]) 
    -2     4 
 
>> disp([1 2]./[-2 2]) 
   -0.5000    1.0000 
 
>> disp([1 2].\[-2 2]) 
    -2     1 
 
>> disp([1 2].^[-2 2]) 
     1     4 

The same holds for the two divides and the power operator.

Catenation of vectors is the same way as with scalars:

>> disp([[1 2],[-2 2]]) 
     1     2    -2     2 
 
>> disp([[1 2];[-2 2]]) 
     1     2 
    -2     2 

There does not seem to be a use for the colon with vectors to produce sequences of numbers as with scalars, but it does help in selecting elements from a vector.

>> v=-3:3;
 
>> disp(v) 
    -3    -2    -1     0     1     2     3 
 
>> disp(v(3:5)) 
    -1     0     1 


4.3 Operations of a Vector with a Scalar

Which of our operators work with a scalar and a vector? Let's try them:

  
>> disp(3+[1 2]) 
     4     5 
 
>> disp(3-[1 2]) 
     2     1 
 
>> disp(3*[1 2]) 
     3     6 
 
>> disp(3/[1 2]) 
 
??? Error using ==> / 
Matrix dimensions must agree. 

We got further than before. At least multiply worked but the first divide does not.

>> disp(3\[1 2]) 
    0.3333    0.6667 
 
>> disp(3./[1 2]) 
    3.0000    1.5000 

The first divide does not require a period to indicate element by element operation, but our second divide does require a period if element by element division is required.

>> disp([1 2]/3) 
    0.3333    0.6667 
 
>> disp([1 2]\3) 
         0 
    1.5000 
 
>> disp([1 2].\3) 
    3.0000    1.5000 

The first and third of these are simply scalar division repeated. In the first case we got each element of the vector divided by three. In the last case we got 3 divided by each element in the vector. In the middle case, MATLAB thought we wanted to solve for x and y such that:

1x + 2y = 3

It gave us one such solution arranged as a column vector.


The power operator works the same way as division, but it does not matter whether we raise the elements in the vector to a scalar power or vice versa:

>> disp(3.^[1 2]) 
     3     9 
 
>> disp([1 2]^3) 
 
??? Error using ==> ^ 
Matrix must be square. 
 
>> disp([1 2].^3) 
     1     8 



4.4 Matrices Used with the Operators

We will not demonstrate all the possible operations between matrices and other arrays. Operations of scalars with matrices follow the same behavior that we have seen with vectors.

>> A=[1 2 3 
      2 3 4]; 
 
>> disp(A+2) 
     3     4     5 
     4     5     6 
 
>> disp(A*2) 
     2     4     6 
     4     6     8  
 
>> disp(A/2) 
    0.5000    1.0000    1.5000 
    1.0000    1.5000    2.0000 
 
>> disp(2/A) 
 
??? Error using ==> / 
Matrix dimensions must agree. 
 
>> disp(2./A) 
    2.0000    1.0000    0.6667 
    1.0000    0.6667    0.5000 

Each of the above cases was identical to what we saw with scalars and vectors. We would expect all the other operators to follow as before so we will leave them for you to demonstrate.

How about a matrix with another matrix of the same size? Here again things are pretty much as with operations on a pair of vectors. A few examples should convince us of how element by element operations work:

>> A1=[-1 0 2 
        0 2 4]; 
 
>> disp(A+A1) 
     0     2     5 
     2     5     8 
 
>> disp(A.*A1) 
    -1     0     6 
     0     6    16 
 
>> disp(A./A1) 
 
Warning: Divide by zero 
   -1.0000       Inf    1.5000 
       Inf    1.5000    1.0000 
 
>> disp(A.\A1) 
   -1.0000         0    0.6667 
         0    0.6667    1.0000 



Continue on to Chapter 5
Return to Table of Contents