MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)

       

In this article, you will learn everything you need to know about vectors in MATLAB.
You’ll learn to:

  • Create vectors
    • Make a vector
    • Access elements
    • Define a vector of increasing numbers
    • Create a column vector from a row vector
    • Extract a vector from another one
    • Create cell vectors
  • Concatenate, delete elements, and reverse vectors
    • Concatenate vectors
    • Delete an element from a vector
    • Reverse a vector
  • Find elements in a vector
    • Value in a vector
    • Maximum value of a vector
  • Find an index in a vector
    • Index of a value in a vector
    • Index of the maximum value of a vector
    • Index of a string in a vector
  • Find the size, magnitude, and sum of a vector
    • Get the size of a vector
    • Find the magnitude of a vector
    • Get the sum of a vector from 1 to n of i^2

How to Create a Vector in MATLAB

Make a Vector

There are a few things that know about how to creating a vector in MATLAB:

  • Definition: to define a row vector of numbers is simple. You just have to put the numbers that you want between square brackets. Here is an example of how to do so:
    vector = [1 2 3 4 5]; % row vector
  • Indexing: vectors in MATLAB are not the same as in every other programming language because the indexing starts from one instead of zero, which means that the first value has the index one.
  • Column Vector: to define a column vector, you can either separate every element with a semi-colon (;), or you can define the vector and use the transpose function, as we will see in the following sections:

    columnVector = [1 ; 2 ; 3 ; 4 ; 5]; % column vector

     

Access Elements

  • To access a value in a vector you just need to write the name of the vector followed by the number of the index referencing the value you desire between parentheses.

matlab matlab vector access value example access value example

  • To access the last value, use the keyword end between parentheses; in our case, the value of the last element is 5:

using the end matlab command to get the last value of an array

  • You can also access every element of the vector using a colon:

example of using colon to get every element of a vector

Create a Vector of Increasing Numbers

If the vector you want to define is made of increasing numbers, you can define it by writing the first number and the last number separated by a colon(:):

vector = 1:5;

This definition is exactly equivalent to the one in the previous section.

Create a Column Vector from a Row Vector: Transpose

You can create a column vector from a row vector by using the transpose function, which is a single quotation mark used at the end of the vector. If we take the previous example, we have:

vectorTranspose = vector'; % vectorTranspose is a column vector

Extract One Vector from Another

You can define a new vector based on a previous one. For example, let’s say that you have the following vector:

v=\begin{pmatrix} 100 & 101 & 102 & 103 & 104 & 105\end{pmatrix}

  • Extract based on indexes: you can define that vector and extract the values let’s say from the third element to the end of the vector:
    v = 100:105;
    vExtracted = v(3:end);

    In MATLAB, you get
    create an array by extracting the values from another one

  • Extract based on values: you can define a rule for the extraction inside the parentheses of the vector that is extracted. For example, if you want to get values that are above 103, you can do so as the following:

create array from another one by extracting above a value

 

MATLAB Cell Vector

You can also define vectors of cells in MATLAB. Here’s how to define and access elements in a cell vector:

  • Defining a cell vector: cells vectors are essential in MATLAB if you want to manipulate strings. Indeed, when you want to define a vector of strings, you have to use cells to separate the different strings from each other; otherwise, it will concatenate every string element of that vector. Here’s how to define a vector of strings:
    vectorOfStrings = [{'cost'} {'speed'} {'size'}];
  • Accessing an element: you then can access every element in the same way you can access them using vectors of numbers, except that instead of using parentheses containing the index of the value you desire, you use braces. For example, if you want to access the string value «speed,» you can do the following:

Example of an array of cells

Concatenate, Delete Elements, and Reverse MATLAB Vectors

Concatenate Vectors

If you want to concatenate vectors in MATLAB, you just need to put them next to one another.

  • Concatenate vectors of numbers: let’s take the example of the vectors “v” and “vector” that we defined earlier as follows:
    vector = 1:5;
    v = 100:105;

    If you want to concatenate them, here’s how to do so:

    newVector = [vector v]; % row vector
  • Concatenate column vectors: to concatenate column vectors, you can put one below the other:

    newColumnVector = [vector' ; v']; % column vector

     

  • Concatenate vectors of strings: This is where cells come in handy. If you want to concatenate vectors of strings and vectors of numbers, you will have to use cells. Here’s an example of concatenating vectors of numbers and vectors of strings:
    cellVector=[{1} {2} {3}];
    vectorOfStrings = [{'cost'} {'speed'} {'size'}];
    newCellVector = [cellVector vectorOfStrings];

    Here’s what you get in MATLAB:

    Example of cells arrays of strings and numbers concatenated

Delete an Element from a Vector

In MATLAB, you can delete an element from a vector by replacing it with empty square brackets using the following:

vector(index) = [];

This will delete the value that is referenced by the index between parentheses. For example:

vector = 1:5;
vector(3) = [];

In MATLAB, you get:

Example deleting a value from a vector

Reverse a Vector

You can also reverse every value of a vector by using the fliplr MATLAB command:

vector = 1:5;
reversedVector = fliplr(vector);

In MATLAB, you get

reverse matlab vector using fliplr matlab command

Find Elements in a Vector

Find a Value in a Vector

To Find a value in a vector, you can write a condition to the right of the vector, between the parentheses. For example, let’s say that you have the following vector and you want to find the values that are above or equal to 2 and that are below 4:

vector = 1:5;

You can then define a condition into the parentheses of your vector (see this article on conditional statements if you want a more thorough explanation of logical operators). For example, this would be:

extract vector using boolean logic

Find the Maximum Value of a Vector

To find the maximum value, you can simply use the max MATLAB command as follows:

maximumValue = max(vector)

Find an Index in a Vector

Find the Index of a Value in a Vector

if you want to find the index of a specific value, you can use the MATLAB command find, to do that:

Index = find(vector==desiredValue);

For example, if you want to find the value 5, here is how to do so:

vector = 1:5;
Index = find(vector==5);

In MATLAB, you get

find index of the element in an array

Which means that the index of the element that has the value 5 in the vector is 5.

Find the Index of the Maximum Value of a Vector

Then, you can combine finding the index of a value with finding the maximum value of a vector in order to find the index of the maximum value of the vector:

vector = 1:5;
maximumValue = max(vector);
IndexOfMaximumValue = find(vector==maximumValue);

Note that if you have several maximum values that are equals, the find MATLAB command will return a vector containing the indexes of the maximum values. In MATLAB, you get the following:

index of the maximum value of an array

Find the Index of a String in a Vector

If you want to find the index of a string in a vector, you can:

  1. Use the strcmp MATLAB command to create a vector of logical Boolean values. The elements of that vector will be 1 if they correspond to the string you’re looking for and 0 if they don’t.
  2. Use the find MATLAB command on that vector.

Here’s an example of how to do this:

vectorOfStrings = [{'cost'} {'speed'} {'size'}];
index = find(strcmp(vectorOfStrings, 'speed'));

In MATLAB, you get
index of the string with the desired value in the array

MATLAB Vector: Size, Magnitude, and Sum

Get the Size (i.e. Number of Data) of a Vector

For a vector, there are two ways to get the number of elements:

  1. Using numel: the MATLAB command numel will count the number of elements in your vector. Here’s how to use it:
    get the size of an array using numel
    Which means that the vector “vectorOfStrings” contains three elements.
  2. Using size: the MATLAB command size will give you the number of rows and columns. However, the reason why I use the numel MATLAB command for vectors is that size will output a vector of two elements. The first element is the number of rows and the second is the number of columns. Sometimes, I get confused and don’t take the right element of the output of the size MATLAB command, so I get the wrong number.

Get the Magnitude of a Vector

You can calculate the magnitude of a vector by using the norm MATLAB command, which will calculate the square root of the sum of every squared element:

 \sqrt{vector\times vector^T}

For example,

a = sqrt(vector*vector');
b = norm(vector);

As you can see, a and b have the same value:

norm of matlab array using the square root functionmagnitude of an array using the norm matlab command

Get the Sum of a Vector: Sum from 1 to n of i^2

  • Sum every element: use the sum MATLAB command to get the sum of every element in a vector:
    i = 1:100;
    sumOfI=sum(i); % get the sum of every element in the vector i
  • Sum every element squared: to apply an operation to every element with the sum MATLAB command, you can use «.». You can then calculate the sum from 1 to 100 of i squared:
    i = 1:100;
    S = sum(i.^2);
  • Using symsum: I won’t go into too much detail here because it’s outside the scope of this article, but there is also another solution if you want to calculate the sum from 1 to 100 of i squared by using the symsum function (you need the Symbolic Math Toolbox for this to work):
    syms i
    S = symsum(i^2, i, [1 100]);

Key takeaways:

  1. Vector definition: use square brackets to define a vector of numbers and braces to define a vector of cells. Use cells for strings, and use the single quotation mark to create a column vector from a row vector:
    columnVector = rowVector';
  2. Concatenate vectors by placing them next to one another, delete an element by using empty square brackets, and reverse a vector using the fliplr MATLAB command.
  3. Find elements in a vector using Boolean conditional statement, and find the maximum value of a vector using the max MATLAB command.
  4. Find indexes in a vector by using the find MATLAB command:

    index = find(vector==desiredValue);
  5. Get the size of a vector using the numel MATLAB command, get the magnitude using the norm and get the sum using the sum MATLAB command.