Site icon Real Technology Tools

MATLAB Vector Tutorial: Create, Add, Concatenate, and Extract

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

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:

Access Elements

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}\)

 

MATLAB Cell Vector

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

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.

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:

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

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:

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

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:

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

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:

    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:

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

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.

If you want to learn more about the tools that helped me stop wasting time doing mindless work (such as generating Excel reports, Word documents, or creating clean and simple user interfaces) I wrote a small reference book about it:

 

 

Exit mobile version