Matlab is a programming platform used to analyze and design systems and products created by engineers and scientists that impact our world significantly. It operates using the matrix-based language called the MATLAB language. It provides the space to operate the most natural expression of computational mathematics. 

Let us look at some of the fundamentals of vectors in Matlab and how to get it started. First, you can start Matlab from the menu if your system runs on Mac OSX or Windows. If you are using a Unix system, type the command Matlab to the Unix shell to activate the software. By now, the software will be open. You can enter your commands here. Commands follow with the signs (>>). Any line starting with the (>>) in the following text also refers to a command line. 

What are Matlab Vectors?

A vector in Matlab refers to a matrix. This matrix may have either one row or one column. There is a distinct difference between rows and columns. When there is a mix-up in the rows and columns, it can lead to many programming errors. 

The major uses of Matlab vectors are to create x-y plots which cannot be represented using linear algebra. Thus, vectors become a convenient data structure. It enforces the rules of linear algebra in its operations. Therefore it is important to keep an eye out for detail for vector creation and manipulation.

Master Data Science and Unlock Top-Tier Roles

With the Data Scientist Master's ProgramStart Learning
Master Data Science and Unlock Top-Tier Roles

Types of Matlab Vectors

There are two types of vectors, which are: 

  • Row Vector 
  • Column Vector

Row Vector

As the name suggests, it is a horizontal set of elements represented within square brackets. The elements within the square brackets are then separated by a space or comma.                                                                                   

X = [ 5 6 9 ] or X = [ 5 , 6 , 9 ]

Column Vector

A column vector represents a vertical set of elements represented within square brackets. One can write a column vector in two ways. The first way is to separate each element by a semicolon. The second way is to write each element on the next row in the command window.

X = [ 7 ; 5 ; 9 ] or

X = [ 7

5

9 ]

Create a Matlab Vector

Let us now look at how to create a Matlab vector. First, creating a vector introduces a vector sign on the left-hand side of an equal sign, and the right-hand side of the equal sign must be evaluated as a vector. There is more than one way to create a vector. Each situation calls for a particular way. Let us look at them in detail. 

  • The first way is to build it using the zeroes of the built-in function, linspace, and logspace.
  • You can assign mathematical expressions involving vectors. 
  • Vectors can also be created by appending elements to a scalar. 
  • Vector created using colon notation. 

Let us look at some of the methods which are more widely used than the others.

Creating vectors with ones, zeros, linspace, and logspace: Creating vectors with these factors allows users to make vectors with prescribed spacing between the elements and a specific size. 

One has to decide how long the vector has to be to create a vector with one of these functions. Once that is decided, one has to decide if the vector is a row or column vector. 

There are two arguments for the ones and zeros functions. The first is how many rows you want to have in your matrix. The number of columns is the second. Set the relevant argument of ones and zeros to one to generate a row or a column vector.

For example, to create a row vector of length 6, filled with ones, use:

>> x = ones(1,6)

Similarly, to create a column vector of length 6, filled with zeros use

>> y = zeros(6,1)

Vectors with linearly or logarithmically spaced elements are produced by the linspace and logspace functions. Here are some illustrations with MATLAB output.

>> x = linspace(1,5,5)

x = 

1     2     3     4     5

>> y = logspace(1,4,4)

y = 

10  100  1000  10000

The third argument is optional with regards to the linspace and logspace. 

Both linspace and logspace include an optional third argument. The third argument specifies the number of elements to use inside the first and second argument range.

How to Plot Vectors in Matlab?

The built-in function quiver in MATLAB may be used to visualize vectors. The steps for using Quiver to plot vectors are as follows:

If you're working in three dimensions, define the vectors as column vectors with x, y, and z components and store them in a matrix. You may create three vectors, for instance, if their components are (1, 2, 3), (2, 3, 1), and (3, 1, 2) as follows:

vectors = [1 2 3; 2 3 1; 3 1 2];

Create column vectors to represent the vectors' beginning positions and store them in a matrix. For example, if you want to start the first vector at the origin and the other two vectors at points (1, 1, 1) and (2, 2, 2), respectively, you can define them as follows:

startpoints = [0 0 0; 1 1 1; 2 2 2];

Use the quiver function to plot the vectors. The basic syntax of the quiver function is:

quiver(x, y, u, v, scale)

Where x and y are the coordinates of the starting points of the vectors, u and v are the components of the vectors, and scale is an optional scaling factor that controls the length of the vectors.

To plot the vectors defined in step 1 and 2, you can use the following code:

quiver(startpoints(:,1), startpoints(:,2), vectors(:,1), vectors(:,2), scale);

Note that if you are working in 3D, you need to include the z components of the vectors and the starting points in the quiver function as well.

Customize the plot as needed. You can change the color and width of the vectors, add labels and a title, adjust the scaling, and so on. Here are some examples:

% Change the color and width of the vectors

quiver(startpoints(:,1), startpoints(:,2), vectors(:,1), vectors(:,2), scale, 'LineWidth', 2, 'Color', 'r');

% Add labels and a title

xlabel('X');

ylabel('Y');

title('Vector Plot');

% Adjust the scaling

axis([xmin xmax ymin ymax]); % set the limits of the plot

axis equal; % make the x and y axis scales equal

These basic steps are to plot vectors in MATLAB using the quiver function. You can also use other functions, such as an arrow and compass, depending on your needs.

Master Data Science and Unlock Top-Tier Roles

With the Data Scientist Master's ProgramStart Learning
Master Data Science and Unlock Top-Tier Roles

Referencing the Elements of a Vector

In MATLAB, you can reference the elements of a vector using indexing. Indexing allows you to access and manipulate individual elements of a vector and subsets of elements. Here are the details on how to reference the elements of a vector in MATLAB:

Indexing Syntax: The indexing syntax for a vector is as follows:

vector(index)

Where vector is the name of the vector and index is the index of the element you want to access. The index can be a single integer or a vector of integers.

Single Element Access: To access a single element of a vector, specify the index of the element you want to access. For example, if you have a vector v with elements [1, 2, 3, 4, 5], and you want to access the second element, you can use the following code:

v(2)

This code returns the value 2, which is the second element of the vector.

Multiple Element Access: To access multiple elements of a vector, you can use a vector of indices. For example, if you want to access the second and fourth elements of the vector v, you can use the following code:

v([2, 4])

This code returns the values [2, 4], which are the second and fourth elements of the vector.

Range of Elements Access: You can access a range of elements in a vector using the colon operator :. For example, if you want to access the first three elements of the vector v, you can use the following code:

v(1:3)

This code returns the values [1, 2, 3], which are the first three elements of the vector.

Modifying Vector Elements: You can modify the elements of a vector by assigning new values to the desired index or range of indices. For example, if you want to change the second element of the vector v to 10, you can use the following code:

v(2) = 10

This code modifies the second element of the vector v to 10.

Operations on Vector Elements: You can perform operations on the elements of a vector by referencing the elements in operation. For example, if you want to calculate the sum of the first three elements of the vector v, you can use the following code:

sum(v(1:3))

This code returns the value 6, which is the sum of the first three elements of the vector.

These are the basic ways to reference the elements of a vector in MATLAB. Indexing can also be used for more complex operations, such as conditional and logical indexing.

Addressing Vector Elements

Addressing vector elements in details

In MATLAB, addressing vector elements is a way of referencing and manipulating individual elements of a vector. Addressing vector elements is done using indexing, which allows you to access and modify individual elements of a vector or subsets of elements. Here are the details on how to address vector elements in MATLAB:

Indexing Syntax: The indexing syntax for a vector is as follows:

vector(index)

where vector is the name of the vector and index is the index of the element you want to access. The index can be a single integer or a vector of integers.

Single Element Addressing: To address a single element of a vector, specify the index of the element you want to address. For example, if you have a vector v with elements [1, 2, 3, 4, 5], and you want to address the second element, you can use the following code:

v(2)

This code returns the value 2, which is the second element of the vector.

Multiple Element Addressing: To address multiple elements of a vector, you can use a vector of indices. For example, if you want to address the second and fourth elements of the vector v, you can use the following code:

v([2, 4])

This code returns the values [2, 4], which are the second and fourth elements of the vector.

Range of Elements Addressing: You can address a range of elements in a vector using the colon operator :. For example, if you want to address the first three elements of the vector v, you can use the following code:

v(1:3)

This code returns the values [1, 2, 3], which are the first three elements of the vector.

Modifying Vector Elements: You can modify the elements of a vector by assigning new values to the desired index or range of indices. For example, if you want to change the second element of the vector v to 10, you can use the following code:

v(2) = 10

This code modifies the second element of the vector v to 10.

Operations on Vector Elements: You can perform operations on the elements of a vector by addressing the elements in the operation. For example, if you want to calculate the sum of the first three elements of the vector v, you can use the following code:

sum(v(1:3))

This code returns the value 6, which is the sum of the first three elements of the vector.

Conditional Indexing: You can use conditional indexing to address elements of a vector that meet a certain condition. For example, if you have a vector v with elements [1, 2, 3, 4, 5], and you want to address the elements that are greater than 3, you can use the following code:

v(v > 3)

This code returns the values [4, 5], which are the elements of the vector that are greater than 3.

Logical Indexing: You can use logical indexing to address elements of a vector based on logical values. For example, if you have a vector v with elements [1, 2, 3, 4, 5], and you want to address the elements that are even, you can use the following code:

v(mod(v, 2) == 0)

This code returns the values

Increasing the Size of a Vector

To increase the size of a vector in MATLAB, you can use several methods. Here are the details on how to increase the size of a vector in MATLAB:

Preallocating Vector: The most efficient way to increase the size of a vector in MATLAB is to preallocate the vector before populating it with data. Preallocation involves creating an empty vector of a known size, then assigning values to the vector as needed. For example, to preallocate a vector of size 10, you can use the following code:

v = zeros(1, 10);

This code creates a vector v of size 10, with all elements initialized to zero.

Appending Elements to a Vector: You can append elements to the end of a vector using the concatenation operator []. For example, to add a single element to the end of a vector v, you can use the following code:

v = [v, new_element];

This code appends the new element to the end of the vector v.

Concatenating Vectors: You can concatenate two vectors to create a larger vector. For example, to concatenate a vector v1 of size 5 with a vector v2 of size 3, you can use the following code:

v = [v1, v2];

This code concatenates the two vectors v1 and v2 to create a new vector v of size 8.

Reshaping Vector: You can reshape a vector to a new size using the reshape function. The new size must have the same number of elements as the original vector. For example, to reshape a vector v of size 8 to a new size of 4 by 2, you can use the following code:

v = reshape(v, 4, 2);

This code reshapes the vector v to a new size of 4 by 2.

Using Expandable Data Structures: MATLAB has several expandable data structures that can be used to increase the size of a vector dynamically. Examples of these data structures include cell arrays and dynamic arrays. These data structures allow you to add or remove elements at any position in the structure, and can be useful when the size of the vector is not known in advance.

Using the repmat Function: You can use the repmat function to repeat a vector or matrix multiple times along specified dimensions. For example, to repeat a vector v of size 3, four times along the second dimension, you can use the following code:

v = repmat(v, 1, 4);

This code repeats the vector v four times along the second dimension to create a new vector v of size 3 by 4.

In summary, there are several methods to increase the size of a vector in MATLAB, and the method used will depend on the specific application and requirements of the problem. Preallocating a vector and appending elements to it is a common and efficient method.

Become the Highest Paid Data Science Expert

With Our Best-in-class Data Science ProgramExplore Now
Become the Highest Paid Data Science Expert

Colon Notation

In mathematics, colon notation expresses the relationship between two elements. It is used to show that one element is related to the other in some way, such as being a subset, a superset, or an element of. It is usually written as x : y, where x is the element related to y.

For example, if you have a set A = {1,2,3}, you can write A : {1,2,3} to show that set A is related to set {1,2,3}. This notation can also be used to express relationships between functions and their derivatives, as in f(x) : f'(x), which means that f'(x) is the derivative of f(x)

Assigning Vector Expressions to a Vector

A vector can be assigned to another vector once it has been created. A vector can be created for the expression on the right-hand side of the equal sign if the vector on the left of the equal sign does not exist. 

>> x = zeros(1,6);

>> y = x;

Vector Operations

Vector operations are the different arithmetic functions that can be done on a vector. They are addition, subtraction, scalar multiplication, transpose of a vector, magnitude of a vector, appending vector, vectors with uniformly spaced elements, and vector dot product. 

Vector Functions

We can consider vector functions by looking at an example. Let us create a vector. The vector elements separated by space between brackets are considered equal to a variable. Create this vector by entering the following into the Matlab window. 

a = [1 2 3 4 5 6 9 8 7] 

Matlab should return 

a =

 1 2 3 4 5 6 9 8 7 

For example, a vector is created with elements between 0 and 20 evenly spaced with an increment of 2. 

t = 0:2:20 

t = 

0 2 4 6 8 10 12 14 16 18 20

Vector manipulation is almost as simple as vector creation. First, imagine that you want to multiply each element in vector "a" by 2.

b = a + 2 

b = 

  3 4 5 6 7 8 11 10 9  

These are some examples of vector functions. 

The Exclusive Path to Your Dream Career

Data Science Career GuideGet Your Copy
The Exclusive Path to Your Dream Career

Frequently Asked Questions

1. What are vectors in MATLAB?

A vector in MatLab refers to a matrix that may have a row or column. 

2. How do you write vectors in MATLAB?

Vectors are written as rows and columns. Rows are horizontal while columns are vertical. 

3. Does MATLAB have a vector?

Yes, MatLab has a vector. There are vector operations and vector functions 

4. How do I open a vector in MATLAB?

You can create a vector by enclosing the elements in square brackets like v=[1 2 3 4 5]. It can also be written using commas like v=[1,2,3,4,5].

5. How do you create a vector?

You can create a row vector using square brackets [ ] in MatLab 

6. What is the difference between array and vector in MATLAB?

A vector is a sequential container to store elements, while an array stores a fixed-size sequential collection of elements of the same type.

Summing Up

Vectors in Matlab are widely used in different problems and are one of the fundamental aspects of Matrixes as well. It is widely applied in different fields, including big data and analytics. 

This blog covers the basics of Matlab vectors in a nutshell. If you want to fully understand the core concepts with examples and practical applications, Simplilearn offers the Caltech Post Graduate Program in Data Science for those who want to learn more about Matlab vectors and how they make working with data easier for data professionals. Sponsored by IBM and industry professionals, this course teaches vectors in Matlab and helps you in your journey into the exciting world of big data and analytics.

Data Science & Business Analytics Courses Duration and Fees

Data Science & Business Analytics programs typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Professional Certificate Program in Data Engineering

Cohort Starts: 2 Jan, 2025

7 months$ 3,850
Professional Certificate in Data Science and Generative AI

Cohort Starts: 6 Jan, 2025

6 months$ 3,800
Post Graduate Program in Data Analytics

Cohort Starts: 13 Jan, 2025

8 months$ 3,500
Caltech Post Graduate Program in Data Science

Cohort Starts: 13 Jan, 2025

11 months$ 4,000
Professional Certificate in Data Analytics and Generative AI

Cohort Starts: 13 Jan, 2025

22 weeks$ 4,000
Data Scientist11 months$ 1,449
Data Analyst11 months$ 1,449