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.
Types of Matlab Vectors
There are two types of vectors, which are:
- Row Vector
- Column 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 ]
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
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)
1 2 3 4 5
>> y = logspace(1,4,4)
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.
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 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.
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
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
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
3 4 5 6 7 8 11 10 9
These are some examples of vector functions.
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.
Our Data Scientist Master's Program covers core topics such as R, Python, Machine Learning, Tableau, Hadoop, and Spark. Get started on your journey today!
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 Data Scientist Master’s Program 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.