# Mathematics Matrix Manipulation in MATLAB

This article is simplest guide for matrix manipulation in MATLAB which a programmer need for a regular work. Everything is treated as a matrix in MATLAB. Ex- a=1; which is a scalar means a matrix of one row and one column which contains the element ‘1’. You can read this in article an introduction to MATLAB

# Matrix Manipulation in MATLAB

Use ‘clear all’ command to clear user defined variables from workspace and ‘clc’ for clearing the command window.

## ROW MATRIX

The matrix which contains ‘n’ number of columns but only one row is called row matrix. They can be made in two different ways which is shown below.

The first way is to give space between the elements written in a square bracket i.e. A= [1 2 3]

The second way is to separate the elements by a comma i.e. B= [1, 2, 3]

The result of both is same and is shown in figure 1:

t= -2*pi : pi/100 : 2pi; makes a matrix from -2pi to 2pi with a fixed increment of pi/100.

Now to make a matrix of trigonometric function we first define a variable t as shown above and then use this command i.e. y= cos(t); the result is shown in figure 2.

C= 1:10; makes a row matrix whose first element is ‘1’ and last element is ‘10’. Here we did not define its common difference so by default it is taken as ‘1’.

## COLUMN MATRIX:

The matrix which contains ‘m’ number of rows but only one column is called column matrix. They can be made by using semicolon instead of space in row matrix. E.g. A= [1; 2; 3]; the result is shown in figure 3

## TRANSPOSE OF A MATRIX:

Transpose of a matrix means interchanging the rows and columns of a matrix. To find transpose of a matrix we first define a matrix and then put an apostrophe after it e.g. A= [1 2 3]’. The result is shown in figure 4.

The addition in matlab is by default entrywise i.e. the first element of first matrix gets added to the first element of second matrix. E.g. A= 1: 10; A + A. the result of this is shown in figure 5.

## MATRIX MULTIPLICATION:

In order to multiply two matrices the number of columns of first matrix must be equal to the number of rows of second i.e. if the order of first matrix is (m X n), the order of second must be (n X r). The order of the new matrix will become (m X r)

E.g. A= [1, 2, 3]; B= [4; 5; 6]; C=A*B; the result is shown in figure 6

In order to multiply the matrices entrywise we use ‘.*’ sign (dot star). This means that the first entry will get multiplied with first, second with second and so on. E.g. D=A.*A

## MULTI ROW MULTI COLUMN MATRIX:

In order to get a second row, we use a semicolon after defining first row. This transfers our entries to a new row. E.g. A= [1, 2, 3; 4, 5, 6; 7, 8, 9];

### EYE

The eye command is used to make an identity matrix. The syntax for this command is ‘eye (number of rows, number of columns)’. E.g. B= eye (3, 3); the result of this is shown in figure 7:

### MAGIC

The magic command is used to make random square matrices. The syntax for this command is ‘magic (number of rows and column). E.g. C= magic (4); the result of this is shown in figure 7:

### PASCAL

This command is used to make symmetric square matrices. The syntax for this command is ‘pascal (number of rows and columns)’. E.g. pascal (4); the result of this is shown in figure 7:

### SIZE

This command gives us the size of a matrix. The syntax for this command is ‘size (variable)’. E.g. A = 1:10; size (A). The result of this is shown in figure 7:

## HIGHER POWER OF MATRIX:

In order to get higher powers of a matrix we use ’. ^’. This command is entrywise i.e. in result higher power of each element would be obtained. The syntax for this command is “A. ^power”. E.g.  A=1:10; A. ^2; will give us result as shown in figure 8:

### ZEROS

This command gives a null matrix i.e. each element of this matrix would be zero. The syntax for this command is A= zeros (number of rows, number of columns). E.g. the result of command zeros (3, 3) is shown in figure 9:

### ONES

This command gives us a matrix who’s each element would be one. The syntax for this command is A= ones (number of rows, number of columns). E.g. the result of command ones (3, 3) is shown in figure 9:

### SCALAR MULTIPLICATION

For scalar multiplication we use ‘.*’ command between two matrices. This multiplication is entrywise i.e. the first element of first matrix gets multiplied with the first element of second matrix and so on. e.g. after the command A= [ 1,2,3 ; 4,5,6 ; 7,8,9 ] ; B= magic (3); the result will be as shown in figure 9:

### INVERSE OF A MATRIX

This command is used to find inverse of a matrix. The syntax of this command is ‘inv (matrix)’. E.g. after the command A= magic (3); inv (A); gives us the result as shown in figure 9:

The scalar product of a matrix and its inverse is not an identity matrix while the matrix product is identity matrix. E.g.  A= magic (3); A.*inv (A); gives us the result as shown in figure 17. While A*inv (A); gives us the identity matrix as shown in figure 9:

## ACCESS A SPECIFIC ENTRY OR COLUMN OR ROW

To excess a specific entry just write matrix name along with row and column number. E.g. after execution of command A= magic (5); A (2, 2); the result obtained would be as shown in figure 10:

To access a specific column write matrix name with colon in place of rows number and specific column number. E.g. after execution of command A (:, 3); the result obtained is shown in figure 11:

To access a specific entry write matrix name with specific row number and colon in place of column number. E.g. after execution of command A (4, :); the result obtained is shown in figure 11