Introduction

Matlab Logo 1-D Arrays (also known as vectors) are commonly used within Matlab, so it is a good idea to understand how they work and how to bend them to your will. This is a quick tutorial on some simple tricks that you may or may not know about vectors.

Creating Vectors

  1. How to create a row vector that increments by 1. For example, let’s create a row vector that goes from 1 to 10, with increments of 1.

    myVector = [1 2 3 4 5 6 7 8 9 10]; %the hard way
    myVector = 1:10 %the easy way
  2. How to transform a row vector to a column vector, and vice versa.

    myVector = 1:10; %creates a row vector 
    myVector = myVector'  %this is the complex conjugate transpose
    myVector = myVector.'  %is the non-conjugate transpose
  3. Note: Dan Kominsky pointed out that is a subtle but important difference here. When you are working with real numbers the difference is irrelevant, but when you are dealing with complex numbers, the meaning is entirely different! Thanks for the correction, Dan.

  4. How to create a column vector that increments by 1. For example, let’s create a column vector that goes from 1 to 10, with increments of 1.

    myVector = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]; %the hard way 
    myVector = [1:10].' %the easy way
  5. How to create a vector that increments by a specific value. Let’s create a vector that goes from 1 to 19, and increments by 2. Note that the increment value is not limited to integers.

    myVector = 1:2:19
  6. How to create a vector that decrements by a specific value. Let’s create a vector that goes from 10 to 1, and decrements by 1

    myVector = 10:-1:1
  7. How to create a vector with equally spaced points. Let’s create a vector that goes from 0 to 100 with 21 equally spaced points.

    %first argument is the start value of the vector
    %second argument is the end value of the vector
    %third argument is the number of points within the vector
    myVector = linspace(0,100,21)
  8. How to create a vector of zeros. For example, let’s create a vector of 10 zeros.

    %first argument is the number of rows
    %second argument is the number of columns
    rowZeros = zeros(1,10)

    Note: Incidentally, this is a great way to preallocate a vector. Preallocating a vector is most useful when FOR loops are involved. Preallocating a vector is preferred over resizing a vector repeatedly as it reduces the processing time.

  9. How to create a vector of ones. For example, let’s create a vector of 10 ones.

    %first argument is the number of rows
    %second argument is the number of columns
    rowOnes = ones(1,10)

    Note: This is yet another way to preallocate a vector.

  10. Adding, Removing, and Replacing Elements within a Vector

  11. How to append a vector. For example, lets add 11 to the end of the vector

    myVector = 1:10;
    myVector = [myVector 11]
     
    %we can also add 11 to the beginning of the vector
    myVector = [11 myVector];

    Note: This method of appending vectors should not be used within large FOR loops. When resizing arrays, memory must be reallocated with a larger size. If this is done repeatedly, there is a speed penalty.

  12. How to append two vectors together.

    myVector1 = 1:5;
    myVector2 = 6:10;
    myVectorAppend = [myVector1 myVector2]
    %myVectorAppend = cat(2,myVector1,myVector2) does the same thing

    Note: Same warning as above.

  13. How to remove a particular element from a vector. Lets say we want to remove the 4th entry.

    myVector = 1:10;
    myVector(4) = []
  14. How to replace a particular element with a different element within a vector. Lets say we want to replace the 4th entry with the value of 100.

    myVector = 1:10;
    myVector(4) = 100
  15. How to remove the last element from a vector.

    myVector = 1:10;
    myVector(end) = []
  16. How to remove the last 5 elements.

    myVector = 1:10;
    myVector(end-4:end) = []
  17. How to keep the last 5 elements (or equivalently, remove the first five elements).

    myVector = 1:10;
    myVector = myVector(end-4:end)
     
    %the following command does the same thing
    myVector(1:5) =[];
  18. How to remove a series of elements. For example, let’s remove entries 3 through 6:

    myVector = 1:10;
    myVector(3:6) = []
  19. How to keep a series of elements. For example, let’s keep entries 3 through 6:

    myVector = 1:10;
    myVector = myVector(3:6)
  20. How to remove a group of specific elements. For example, lets remove entries 2,5, and 7:

    myVector = 1:10;
    myVector([2,5,7]) = []
  21. How to keep a group of specific elements. For example, lets keep entries 2,5, and 7:

    myVector = 1:10;
    myVector = myVector([2,5,7])
  22. How to get the number of elements within a vector. Useful when creating a for loop to run through a vector.

    myVector = 1:10;
    numElements = length(myVector)
     
    %the following command does the same thing
    numElements = numel(myVector)
  23. How to remove all zeros from a vector.

    myVector = [0 0 0 1 2 3 0 0 4 5 1 2 0 0];
     
    %index contains the indices elements within myVector which are non-zero
    index = find(myVector);
    myVector = myVector(index) %removes all the zeros within the vector

    Alternatively, logical indexing can be used (and is more efficient)

    myVector(myVector == 0) = [];
  24. How to remove a particular value from a vector. For example, how to remove any occurence of 6 within a vector

    myVector = [6 6 0 1 2 3 0 0 6 6 1 2 0 0];
     
    %index contains the indices of elements within myVector which are equal to 6
    index = find(myVector == 6 );
    myVector(index) = []

    Alternatively, logical indexing can be used (and is more efficient)

    myVector(myVector == 6) = [];
  25. How to remove the first two occurences of 6 within a vector

    myVector = [6 6 0 1 2 3 0 0 6 6 1 2 0 0];
    index = find(myVector == 6,2);
    myVector(index) = []
  26. How to remove all elements greater than 5 from a vector.

    myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];
     
    %index contains indices of elements within myVector which are greater than 5
    index = find(myVector > 5);
    myVector(index) = []

    Alternatively, logical indexing can be used (and is more efficient)

    myVector(myVector > 5) = [];
  27. Similarly, how to remove all elements less than 5 from a vector.

    myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];
     
    %index contains the indices of elements within myVector which are less than 5
    index = find(myVector < 5);
    myVector(index) = []

    Alternatively, logical indexing can be used (and is more efficient)

    myVector(myVector < 5) = [];
  28. Sorting and Shifting Vectors

  29. How to reverse a vector.

    myVector = 1:10;
    myVector = myVector(end:-1:1)
  30. How to sort a vector.

    myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];
    myVectorAscend = sort(myVector) %sort ascending
    myVectorDescend = sort(myVector,'descend') %sort descending
  31. How to shift the elements one spot to the right.

    myVector = 1:10;
    myVector = myVector([end 1:end-1])
  32. How to shift the elements one spot to the left.

    myVector = 1:10;
    myVector2 = myVector([2:end 1])
  33. Other useful commands:

  34. How to get the maximum and minimum value of a vector

    myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];
    maxValue = max(myVector); 
    minValue = min(myVector);
  35. How to add up all the elements within a vector.

    myVector = 1:10;
    total = sum(myVector);
  36. How to get the product of all the elements within a vector.

    myVector = 1:10;
    total = prod(myVector);
  37. How to get the average, standard deviation, and variance of a vector.

    myVector = 1:10;
    averageArray = mean(myVector)
    stdArray = std(myVector)
    varArray = var(myVector)

Got any tricks up your sleeve?

There are a ton of Matlab tricks that were not covered in this tutorial. Do you know of any super useful trick? If you have any of your own tips and tricks up your sleeve, please share at least one!