Vectorized (or Array) Operations

Vector operations in Matlab allow you to apply a "single" command to an entire array. In fact what is happening is that "single" command is applied over and over again to every element of the array. Vectorized operations are equivalent to for loops and all vectorized operations can be replaced with for loops.

Vector (or Array) Operations

A "Vector" operation in Matlab is the ability to write condensed code to apply an action to every element of an array with a single line of code.

We all know the basic operators in Math: +, -, *, /, etc.

5 + 5 produces 10;

Vectorized operators look like these basic math operators and generally do "almost" the same thing. But when applied to an Array (or matrix), these operations are performed over EVERY element of the array (very similar to our notion of a "loop").

Here are some examples:

  1. Adding a single value to every element in an array:

                
              [5 6 7] + 3; % == [8 9 10]
                
              
  2. Adding two arrays together (element by element):

                
                  [5 6 7] + [1 2 3]; % == [6 8 10]   
                
              

Do not confuse vector operations with geometric vectors. Geometric vectors represent an X and Y direction in space. Vectorized operations apply a single command to every element of an array. Geometric vectors are called Vectors, because they are, in essence, "arrays of directions".


Vector Operators

The + sign works directly as a "vector operation" and as a "scalar operation" (applying to a single value). Some operators have two meanings and thus requires a special (new) operator for vector math.

  1. Division

    Dividing every element by a single value is accomplished just using the / for division.

                
                  [5 6 7] / 10
                  ans =
                     .5000 .6000 .7000
                
              

    Dividing every element in an array by a value in a corresponding array is done using the ./ (dot slash) notation.

                
                  [5 6 7] ./ [ 8 9 10 ]
                  ans =
                     0.6250   0.6667   0.7000
                
              

    Matrix division which is roughly the same as A times the inverse of B

                
                  [5 6 7] / [ 8 9 10 ]
                  ans =
                     0.6694
                
              
  2. Multiplication

    Multiplying every element by a single value is accomplished just using the *.

                
                  [5 6 7] * 10
                  ans =
                     50 60 70
                
              

    Multiplying every element in an array by a value in a corresponding array is done using the .* (dot slash) notation.

                
                  [5 6 7] .* [ 8 9 10 ]
                  ans =
                     40   54   70
                
              

    Matrix multiplication uses the * notation.

                
    	      % Note: the ' means matrix transpose!
                  [5 6 7] * [ 8 9 10 ]'  
                  ans =
                     0.6694
                
              
  3. Other Operations

    Power (^) and other operators generally work in a similar method. See the online Matlab help.


Colon Operator

As stated elsewhere, the colon is used to build a list of values (an array) based on a "start value" : "increment value" : "end value"

If an "increment value" is not given, then 1 is assumed.

        

          array = 5:7     % produces [5 6 7]
          array = 1:.5:4  % produces [ 1 1.5 2 2.5 3 3.5 4 ]
          
        
      

Vector Math vs. For Loops

As we have talked about before, FOR loops are used to execute a section of code a known number of times. For example, say we have a list of quiz scores and we decide to curve the final scores by 5 points. We must add 5 points to every quiz. The following use of the FOR loop will achieve this goal:

        
quiz_scores = [88, 98, 87, 76, 89, 93, 95];

for index = 1 : length(quiz_scores)
  quiz_scores(index) = quiz_scores(index) + 5;
end

  % the old value of the current index into the array is added
  % to 5 and then stored in the same "bucket" overwriting the 
  % previous value
        
      

Because this type of operation is so common Matlab has a special vector operation notation.

        
quiz_scores = quiz_scores + 5;
        
      

Examples of Converting Between For Loops and Vector Notation

  1. Here we covert a vectorized addition in to a for loop:

    Using Vector Operations

    	    
                array = [5 6 7];
                array + 3;  % == [8 9 10]
    	    
              

    Using a For Loop

    	    
                for i=1:length(array)
                  array(i) = array(i) + 3;
                end
    	    
              
  2. Addition of two arrays (of the same length):

    Vector Op

                
           array_1 = [5 6 7];
           array_2 = [1 2 3];
           array_3 = array_1 + array_2;  % == [6 8 10]
                
              

    Using a For Loop

                
       array_3 = zeros(size(array_1));  % preallocate 'buckets' for array
    
       for i=1:length(array_1)
         array_3(i) = array_1(i) + array_2(i);
       end
                
              
  3. Example of a For loop to replace the sum function (which is somewhat similar to a vector operation.)

    Vector Operation

                
       array_1 = [5 6 7];
       result = sum( array_1 );  % result == 18;
                
              

    Using a For Loop

                
       result = 0;  % initialize result.
    
     
         % add the current value in the result variable to the current
         % value in the array (at position i) and store this new value
         % in the result varaible (overwriting the old value)
    
    
       for i=1:length(array_1)
          result = result  + array_1(i);
       end
                
              


Back to Topics List