Column Vectors in MATLAB
Column vectors are vectors that have a single column but, multiple rows. MATLAB provides various functions that use column vectors as their arguments. In this article, we will see different methods of creating and using column vectors in MATLAB.
Creating Column Vectors:
Method 1:
The simplest way of creating column vectors in MATLAB is by using the ‘;’ separator. See the example below.
Example 1:
Matlab
% MATLAB Create Column Vectors vec = [1;2;3;4;5] |
Output:
This will create a column vector with 5 rows.
Method 2:
A column vector is the transpose of a row vector so, we can convert a row vector into a column vector by taking its transpose.
Example 2:
Matlab
% MATLAB code for creating a row vector vec = 3:13; % Displaying the row vector disp(vec) % Computing the transpose of vec vec = vec'; % Displaying the transpose of row vector vec disp( "Transpose of row vector is:" ) disp(vec) |
Output:
Uses of Column Vectors:
One of the uses of column vectors is for multiplying row vectors.
Consider the case when we are given two row vectors and we need to find their product. Since they are both row vectors, it is impossible to calculate their product as their dimensions are not compatible. So, we would change one of them to a column vector and then we can compute their product.
Example 3:
Matlab
% MATLAB Code for % Two row vectors vecA = 1:8; vecB = 5:12; % Converting vecB into a column vector vecB = vecB'; % Calculating the product of column % vector vecB and row vector vecA prod = vecB*vecA; disp(prod) |
Output: