Vectors and Matrix Algebra in MATLAB | MATLAB Tutorial
by Ratnesh Shah
Vectors and Matrix Algebra in MATLAB
Variables containing numerical values can be either represented as a scalar or as vectors. Thus whenever it is a single value, it is known as a scalar, and when many numerals are addressed jointly, it is an array. MATLAB facilitates us to make use of both of these variables with a lot of ease.
Vectors are those arrays which have values arranged in the form of list. As a consequence, we generally call these as 1-dimensional arrays. There are two kinds of vectors, a row vector and a column vector. A row vector has all its values arranged in a horizontal line, and a column vector has values in vertical alignment. Moreover, a typical vector can be presented as follows:
x = [1 2 3 4]
Figure 1 shows how to create a row vector and a column vector respectively.
Figure 1: vector declaration
The size of these vectors will be known as 1×4 and 4×1 vectors. In addition to, the convention of describing the size of an array is generally m×n, where m refers to the number of rows and n refers to the number of columns.
Therefore, different elements of the vector can be accessed by using the variable name and the address of the particular element. Furthermore, address of the element is also known as index, which means the relative positioning of the element from the beginning. In the example given in figure, there is a vector of dimension 1×6. If we want to access the 5th element, we can do so by using index written inside closed parenthesis. x(5)
Figure 2: Indexing a vector element
Consequently, if we want to select a range of elements, we can do so by using the colon notation.
start:end or start:increment:end
First type of indexing selects all the elements in the range, but in the second one, elements are selected by skipping ’increment’ number of elements. Figure 3 shows the correct usage.
Figure 3: Indexing a range of elements
Operations on vectors
Combining two vectors
>>A = [22,07]; >>B = [1991,01]; >>C = [A,B] C = 22 7 1991 1
Extending a vector
An already existing vector with n elements can accommodate more elements by simply assigning these new elements with the new index.
>>A = [22,07]; >>A(3) = 1991 A = 22 7 1991
Finding length of the vector
Following command lists the number of elements currently held in a vector.
>> A = [22,07,1991]; >>length(A) ans = 3
Modifying a vector
Elements of the vector can be modified by directly accessing the location and replacing the existing value with the new one.
>>A = [22,07,1991]; >>A(2) = 03 A = 22 3 1991
Transpose of a vector
Transpose operation on a vector converts a row vector into a column one and vice versa. Transpose operation is performed by using ’ symbol.
>>A = [22,07,1991]; >>B = A’ B = 22 3 1991
A matrix represents a two dimensional array. All the values are arranged in the form of a grid with multiple columns and rows. For e.g.
Typically, a matrix can be declared as in the figure 4. Similar to the vectors, size of the matrix will be conventionally called as n×m, with n being the number of rows and m as number of columns. In the above example, the matrix is of size 3×3. Unlike vectors, size of a matrix can be determined by the function ’size’. Different uses of this function are shown in figure 5.
As we can see, vectors are the specialized case of a matrix, when the size of either of rows or columns is equal to one.
Figure 4: Matrix declaration
Figure 5: Size calculation of a matrix
Addressing elements works the similar way as we do in the case of vectors. Round brackets or parenthesis are used to index a matrix, but the index of a matrix consists of two numbers, the column number and the row number; (row index, column index).
>>A = [1,2,3;4,5,6;7,8,9] A = 1 2 3 4 5 6 7 8 9
>> A(2,3) ans = 6
In order to select a range of elements, which here means selecting a sub matrix, we can use semicolon notation for both column and row indices.
>> A(1:2,2:3) ans = 2 3 5 6
One big advantage of using arrays in MATLAB is that all the lengthy code written for loop operations can be avoided by writing it in one line. Since, any function on a matrix is applied on all the elements of the matrix by just specifying its name.
>>A = [1,2,3;4,5,6;7,8,9] A = 1 2 3 4 5 6 7 8 9
>> sin(A) ans = 0.8415 0.9093 0.1411 −0.7568 −0.9589 −0.2794 0.6570 0.9894 0.4121
It is possible to perform all basic algebra operations on the vectors/matrices and scalars. These operations can be either between a scalar and a scalar, a scalar and an array, an array and array and between corresponding elements of the array. An example usage of all these operations have been described in the table below 1. Let us consider two scalar variables as ’a’ and ’b’, and two vector variables ’A’ and ’B’, for our explanation below.
Table 1: Various algebraic operations
While performing array operations, we need to make sure, the dimensions of arrays are compatible with each other. There is no problem when we are performing scalar-scalar and scalar-array operations, but have to be careful on rest of the cases.
In the case of array-array addition/subtraction and all element wise array operations, it is necessary that the matrices which are involved are of exact same size. If it is not so, there will be error displayed and the code will not execute, figure 6.
Figure 6: Array-array operations on arrays of incompatible dimensions.
Besides this, there are different rules for array-array multiplication and division. While performing array multiplication, it is necessary that the rows of first array and columns of second array should be equal in number. Therefore, if the size of two matrices are M×K and K×N respectively, the resulting matrix after multiplication will be of size M×N.
Figure 7: Array-array multiplication.
On the other hand, there are two kinds of matrix divisions, A/Matrix left division and A\B, Matrix right division. Both of these yield different results, since left division corresponds to the solution for the equation Ax = B, and right division corresponds to xA = B. It is necessary that both matrices have same number of rows in case of left division and they have same number of columns in case of right division.
Figure 8: Left division