# Basic operations on Matrices

Aim: To generate matrix and perform basic operation on matrices Using MATLAB Software.

EQUIPMENTS:

• PC with windows (Windows 7/8/10).
• MATLAB Software

Theory:

%Creating a vector from a Known list of numbers:

``A=input('enter the Matrix/Vector Elements')``

enter the Matrix/Vector Elements 1 2 3 4

Result:

A  =   1    2    3   4

%Creating a vector with constant spacing by specifying the first term, spacing and the last term:

``````m=input('enter the first term')
q=input('enter the spacing')
%Default value of q is 1
n=input('enter the last term')
B=[m:q:n]``````

Result:

enter the first term m =2

enter the spacing q = 0.1000

enter the last term n =4

B  =

column  1 to 12

2.    2.1    2.2    2.3    2.4    2.5    2.6    2.7    2.8    2.9    3.    3.1

column 13 to 21

3.2    3.3    3.4    3.5    3.6    3.7    3.8    3.9    4.

%Creating a vector with constant spacing by specifying the first and the last terms, and the number of terms:

``````a=input('enter the first element')
b=input('enter the last element')
c=input('enter the number of elements')
C=linspace (a,b,c)``````

Result:

enter the first element a =3.

enter the last element b =6.

enter the number of elements c =5.

C = 3.    3.75    4.5    5.25    6

% Creating an All Zero Vector

``````m=input('enter the number of terms in the vector')
D=zeros(1,m)``````

Result:

enter the number of terms in the vector m =5.

D =0     0     0     0     0

% Creating a Vector consisting of all Ones

``````n=input('enter the number of terms in the vector')
E=ones(1,n)``````

Result:

enter the number of terms in the vector n =6.

E =1    1   1    1    1    1

% Measuring the size(number of elements) of the vector

``````A=[1 2 3 4]
l=length(A)``````

Result:

A =1     2     3     4

l = 4.

% Measuring the order of the vector

``````D= [1 1 1 1 1]
p=size (D)``````

Result:

D =1     1    1     1     1

p =1     5

``````disp('The number of rows is')
The number of rows is
disp(p(1))``````

Result:   1.

``````disp('The number of columns is')
The number of columns is
disp(p(2))``````

Result:   5.

% Transpose of a Matrix

``````A=[1 2 3 4]
A1=A'``````

Result:

A = 1.     2.     3.     4.

A1 =

1.

2.

3.

4.

% Multiplying each element of the vector/Matrix with a constant

``````k=input('enter the value of the Multiplying Constant')
B=[3 4 5 6]
B1=k*B``````

Result:

k =3.

B =3.     4.     5.     6.

B1 =9.    12.    15.    18.

%The above Multiplication operation can be done also as follows:

``B11=k.*B``

% Finding the sum of elements of a Matrix

``````A=[1  2.1 3.3  4]
S=sum(A)``````

Result:

A =1.    2.1    3.3    4.

S =10.4

``````B =
1.    2.    3.
4.    5.    6.
7.    8.    9.
S1=sum(B)``````

Result:

S1 = 45.

%Finding the sum of each row and displaying in a column

``S2=sum(B, 'c')``

Result:

S2 =

6.

15.

24.

% Finding the sum of each column and displaying in a row

``S3=sum(B, 'r')``

Result:

S3  =

12.    15.    18.

% Finding the average of the elements of Matrix

``````B=[2  3  4  5]
Ave=mean(B)``````

Result:

B =2.     3.     4.     5.

Ave =3.5

%Finding the maximum and minimum values of a vector

``````A=[1  2 3 10.1 -1]
A1=max(A)
A2=min(A)  ``````

Result:

A  =1.    2.    3.    10.1  - 1.

A1  =10.1

A2= -1

%Finding the product of the elements of a Matrix

``````A=[1  2  3  4]
P=prod(A)``````

Result:

A =1.     2.     3.     4.

P =24.

% Checking the sign of the elements of a Matrix

``````B=[-1 -2 3 4 -5 0]
S=sign(B)``````

% sign(B) returns 1 if the element of B is +ve, -1 if the element  is –ve and zero if the element is zero

Result:

B  =

- 1.  - 2.    3.    4.  - 5.    0.

S  =

- 1.  - 1.    1.    1.  - 1.    0.

% Finding the non zero elements of the Matrix

``````A=[-1  2  0  0  2  3]
S=find(A)``````

% find(A) returns the indices of the non- zero elements of A

Result:

A = -1.     2.     0.     0.     2.     3.

S =1.     2.     5.     6.

% Arranging the elements of a Matrix in ascending/descending order

``````A=[1  23  45  6  73]
A1=sort(A,’ascend’)``````

Result:

A =1.    23.    45.     6.    73.

A1 =1.   6.    23.    45.    73.

% mtlb_sort(A) also will do the same

``A2=sort(A, ’descend’)``

Result:

A2= 73.    45.    23.     6.     1.

%Reshaping  the  Matrix

``````A=[1 2 3 4]
A1=matrix(A,4,1)
A2=A(:)
A3=matrix(A,2,2)``````

Result:

A =1.     2.     3.     4.

A1 =A2=

1.

2.

3.

4.

A3 =

1.     3.

2.     4.

% Extending the Dimension of the Matrix

``````A=[1 2 3]
%Appending a Row
A(2,:)=[4 5 6]``````

Result:

A =1.     2.     3.

A =1.     2.     3.

4.     5.     6.

%%Appending a Column

``````A=[1 2 3]
A(:,4)= 10``````

Result:

A = 1.     2.     3.    10.

%Deleting the elements of a Matrix

``````A=[1 2 3 4]
A =1.     2.   3.     4.
% Deleting the 4th column element
A(4)=[ ]``````

Result:

A =1.     2.    3.

%Arithmetic operations

``````A=[1 2 3]
B=[5 6 7]
S=A+B
D=A-B``````

Result:

A =1.     2.     3.

B =5.     6.     7.

S =6.     8.    10.

D = -4.    -4.    -4.

%Multiplication of Two Matrices

``````A=[1 2 3]
B=[2;3;4]
C=A*B  ``````

Result:

A =1.     2.     3.

B =

2.

3.

4.

C =20

% Element wise Multiplication

``````A=[1 2 3]
B=[3 4 5]
C=A.*B  ``````

Result:

A =1.     2.     3.

B =3.     4.     5.

C =3.     8.    15.

%Element wise Division

``````A=[1 2 3]
B=[3 4 5]
C=A./B  ``````

Result:

A =1.     2.     3.

B =4.     4.     6.

C =0.25    0.4    0.5

``C1=A.\B``

Result:

C1=3.    2.    1.6666667

%Raising each element of the matrix to 3rd power

A=[2  5 8]

B=A.^3

Result:

A =2.     5.     8.

B =8.   125.   512.

%Relational Operators

``````A=[1 2 3]
B=[5 3 1]
K=A>B
K1=A<B``````

Result:

A =1.     2.     3.

B =5.     3.     1.

K = F     F     T

K1=T    T     F

``````A=[1 2 3]
B=[1 3 3]
K=A<=B
K1=A>=B
K2=A==B``````

Result:

A =1.     2.     3.

B =1.     3.     3.

K = T    T     T

K1=T    F     T

K2=T    F     T

``````A=[1 2 3]
B=[1 3 3]
K=A~=B( not equal to)``````

Result:

A  =  1.    2.    3.

B  =  1.    3.    3.

K  =   F T F

%Logical Operators

``````A=[1 2 3]
B=[2 0 4]
K=A|B
K1=A&amp;amp;amp;B``````

Result:

A =1.     2.     3.

B =2.     0.     4.

x =T     T     T

y =T     F     T

``````A=[1 0 3]
K=~A``````

Result:

A=1.     0.     3.

K =F    T     F

%These Programs explain  various (MxN) Matrix Operations:

%Creating a One-Dimensional Array/Vector:

``A=input('enter the Matrix Elements')``

Result:

enter the Matrix Elements[1 2 3;4 5 6;7 8 9]

A =

1.     2.     3.

4.     5.     6.

7.     8.     9.

%creating a Matrix consisting of all zeros

``A=zeros(3,2)``

A =

0.     0.

0.     0.

0.     0.

%creating a Matrix consisting of all Ones

``B=ones(4,2)``

Result:

B =

1.     1.

1.     1.

1.     1.

1.     1.

%Creating an Identity Matrix

``A=eye(3,3)``

Result:

A =

1.    0.     0.

0.    1.     0.

0.    0.     1.

%Measuring the order of the vector

``````A=[1 2 3;4 5 6;2 3 -1]
p=size(A)``````

Result:

A =

1.     2.     3.

``    4.     5.     6.``

2.     3.    -1.

p=  3.     3.

``disp('The number of rows is')``

Result:

The number of rows is

``disp(p(1))``

Result:

3.

``disp('The number of columns is')``

Result:

The number of columns is

``disp(p(2))``

Result:

3.

%Transpose of a Matrix

``````A=[1 2 3 ;10 11 12; 34 45 89]
A1=A'``````

Result:

A =

1.     2.     3.

10.    11.    12.

34.    45.    89.

A1 =

1.    10.    34.

2.    11.    45.

3.    12.    89.

%Multiplying each element of the Matrix with a constant

``````k=input('enter the value of the Multiplying Constant')
B=[3 4 5 6;2 4 6 8]
B1=k*B``````

Result:

enter the value of the Multiplying Constant

k =4

B =

3.     4.     5.     6.

2.     4.     6.     8.

B1 =

12.    16.    20.    24.

8.    16.    24.    32.

%The above Multiplication operation can be done also as follows:

``B11=k.*B``

%Finding the sum of element of a Matrix

``````A=[1  2 ;3 4;5 6]
S=sum(A)``````

Result:

A =

1.     2.

3.     4.

5.     6.

S =21.

%Finding the average of the elements of Matrix

``````B=[2 3 4;4 5 6]
Ave=mean(B)``````

Result:

B =

2.     3.     4.

4.     5.     6.

Ave =

4.

%Finding the maximum and minimum values of a vector

``````A=[1 2 3; 10  2  -1]
A1=max(A)
A2=min(A)
P=prod(A)``````

Result:

A =

1.     2.     3.

10.     2.    -1.

A1 =10.

A2 = -1.

P = -120.

%Checking the sign of the elements of a Matrix

``````B=[-1 -2 3;4 -5 0]
S=sign(B)
%sign(B) returns 1 if the element of B is +ve, -1 if the element  is –ve and zero if the element is zero``````

Result:

B =

-1.    -2.     3.

4.    -5.     0.

S =

-1.    -1.     1.

1.    -1.     0.

%Finding the non zero elements of the Matrix

``````A=[-1 2 0; 0 2 3]
S=find(A)
%find(A) returns the indices of the non- zero elements of A  ``````

Result:

A =

-1.     2.     0.

0.     2.     3.

S = 1.    3.    4.    6.

% checking a matrix whether is empty or not

``````A=[1 2 3 0]
K= isempty(A)
%isempty() returns a true value for an empty matrix``````

Result:

A  =

1.    2.    3.    0.

K  = F

%Arranging the elements of a Matrix in ascending/descending order

``````A=[1 23 45; 6 7 3]
A1=sort(A,’descend’)
%each column of A is sorted``````

Result:

A =

1.    23.    45.

6.     7.     3.

A1 =

6.    23.    45.

1.    7.     3.

``````A1=sort(A, ‘ascend’)
%each row of A is sorted``````

Result:

A1=

45.    23.    1.

7.     6.     3.

%all the elements  of A are sorted in increasing order

A1  =

1.    6.    23.

3.    7.    45.

% all the elements  of A are sorted in decreasing  order

``A1=sort(A, ‘desccend’)``

A1  =

45.    7.    3.

23.    6.    1.

%Reshaping  the  Matrix

``````A=[1 2 3 4;3 5 6 7]
A1=matrix(A,4,2)
A2=matrix(A,1,8)``````

Result:

A =

1.     2.     3.     4.

3.     5.     6.     7.

A1 =

1.     3.

3.     6.

2.     4.

5.     7.

A2 =

1.     3.     2.     5.     3.     6.     4.     7.

%Extending the Dimension of the Matrix

``A=[1 2 3;4 5 6]``

%Appending a Row

``A(3,:)=[10 15 26]``

Result:

A=    1.    2.    3.

4.    5.    6.

A  =

1.     2.     3.

4.     5.     6.

10.    15.    26.

%Appending a Column

``A(:,4)=[10;20;87]``

A= 1.     2.     3.     10.

4.     5.     6.     20.

10.   15.   26.    87.

%Appending an Intermediate  row

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

A =

1.     2.     3.

4.     5.     6.

7.     8.     9.

%Appending a  row between 1st and 2nd rows

``A=[A(1,:); 10 11 12;A(2,:);A(3,:)]``

Result:

A =

1.     2.     3.

10.    11.    12.

4.     5.     6.

7.     8.     9.

%Appending a column between 2nd and 3rd column

``````A=[1 2 3;4 5 6;7 8 9]
A=[A(:,1) A(:,2) [10;12;15;16] A(:,3)]``````

Result:

A =

1.     2.    10.     3.

10.    11.  12.    12.

4.     5.    15.     6.

7.     8.    16.     9.

%Deleting the row/column of a Matrix

``````A=[1 2 3; 4 5 6]
%Deleting the 2nd  row
A(2,:)=[ ]``````

Result:

A =

1.     2.     3.

4.     5.     6.

A =

1.     2.     3.

%Deleting the 2nd column

``````A=[1 2 3; 4 5 6]
A(:,2)=[ ]``````

Result:

A =

1.     2.     3.

4.     5.     6.

A =

.   3.

4.     6.

%Arithmetic operations

``````A=[1 2 3;4 5 6]
B=[5 6 7;10 11 12]
S=A+B
D=A-B``````

Result:

A =

1.     2.     3.

4.     5.     6.

B =

5.     6.     7.

10.    11.    12.

S =

6.     8.    10.

14.    16.    18.

D =

-4.    -4.    -4.

-6.    -6.    -6.

%Multiplication of Two Matrices

``````A=[1 2 3;4 5 6]
B=[5 6 3;10 11 12;3 4 6]
C=A*B``````

Result:

A =

1.     2.     3.

4.     5.     6.

B =

5.     6.     3.

10.    11.    12.

3.     4.     6.

C =

34.    40.    45.

88.   103.   108.

%Element wise Multiplication

``````A=[1 2 3;4 5 6]
B=[3 4 5;3 2 1]
C=A.*B  ``````

Result:

A =

1.     2.     3.

4.     5.     6.

B =

3.     4.     5.

3.     2.     1.

C =

3.     8.    15.

12.    10.     6.

``````%Element wise Division
C=A./B  ``````

Result:

C =

0.3333    0.5000    0.6000

1.3333    2.5000    6.0000

%Raising each element of the matrix to 4th  power

``````A=[2 5 8;4 5 6]
B=A.^4``````

Result:

A =

2.     5.     8.

4.     5.     6.

B =

16.         625.        4096.

256.        625.       1296.

%Relational Operators

``````A=[1 2 3;10 3 5]
B=[5 3 1;0 3 6]
K=A>B
x=A<B
y=A<=B
K1=A>=B
K2=A==B
K3=A~=B``````

Result:

A =

1.     2.     3.

10.     3.     5.

B =

5.     3.     1.

0.     3.     6.

K =

F     F     T

T     F     F

x =

T     T     F

F     F     T

y =

T     T     F

F     T     T

K1 =

F     F     T

T     T     F

K2 =

F     F     F

F     T     F

K3 =

T      T     T

T      F     T

%Logical Operators

``````A=[1 2 3;0 0 1;1 -1 0]
B=[2 0 4;-1 2 -3;9 8 0]
K=A|B
K1=A&amp;amp;B``````

Result:

A =

1.     2.     3.

0.     0.     1.

1.    -1.     0.

B =

2.     0.     4.

-1.     2.    -3.

9.     8.     0.

K =

T   T   T

T   T   T

T   T   F

K1 =

T   F   T

F   F   T

T   T   F

``````A=[1 0 3;2 -1 0;3 2 -1]
K=~A/k=not(A)``````

Result:

A =

1     0     3

2    -1     0

3     2    -1

K =

F   T   F

F   F   T

Viva Questions:

1. What is the command to generate row matrix?

R=[a,b,c]

2. What is the command to generate column matrix?

colvec = [a ; b ; c]

3. What is the command to generate matrix?

matrix=[1,2,3;4,5,6;7,8,9]

4. What is the command to find Inverse matrix?

Inverse_matrix= Inv(m).

• Updated
Feb 12, 2020
• Views
4,292
You May Like