0% found this document useful (0 votes)
8 views6 pages

Matlab 3

Uploaded by

Pranay Kami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Matlab 3

Uploaded by

Pranay Kami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Matrices

Q 1. Implement the syntax for the following :


i ¿Create a ¿ 2 X 3 ¿ ;
ii ¿ show the¿ the ¿
iii ¿ Show the value at 2 nd row :1 st column location of the¿
iv ¿ Replacethat value with0

Ans. a=[1 2 3;4 5 6] // this command creates 2X3 matrix


//using “;” to make a new row.
a=
1 2 3

4 5 6

>> size(a) // this command displays the size of current matrix.


ans =
2 3

>> a(2,1) // this command displays the element of 2nd row and 1st column.
ans =
4

>> a(2,1) = 0 // this command makes the element of 2nd row and 1st column zero.
a=
1 2 3
0 5 6

Q 2. Implement the syntax for the following :


i ¿Create a 3 X 3 ¿ make row 2 ¿ 3 null ;
ii ¿ make row 1¿ 2 zero ;
iii ¿ make column2 ¿ 3 null∧zero respectively .

Ans. a = [1 2 3;4 5 6; 7 8 9] // this command creates 3X3 matrix of given values.


a=
1 2 3
4 5 6
7 8 9

>> a(2:3,:) = [ ] // this command selects the elements from row 2 to 3 and all
columns and make it null.
a=
1 2 3

>> a = [1 2 3;4 5 6; 7 8 0] // this command creates 3X3 matrix of given values.


a=
1 2 3

12
Matrices

4 5 6
7 8 9

>> a(1:2,:) = [0] // this command selects the elements from row 1 to 2 and all
columns and makes it zero.
a=
0 0 0
0 0 0
7 8 0

>> a = [1 2 3;4 5 6; 7 8 9] // this command creates 3X3 matrix of given values.


a=
1 2 3
4 5 6
7 8 9

>> a(:,2:3) = [] // this command selects the elements from column 2 to 3 and all
rows and makes it null.
a=
1
4
7
>> a = [1 2 3;4 5 6; 7 8 9]; // this command creates 3X3 matrix of given values.
>> a(:,2:3) = [0] // this command selects the elements from column 2 to 3 and all
rows and makes it zero.
a=
1 0 0
4 0 0
7 0 0

Q 3. Implement the syntax for taking a opted portion of one ¿ represent as


another ¿

Ans. a = [1 2 3; 4 5 6; 7 8 9] // this command creates 3X3 matrix of given values.


a=
1 2 3
4 5 6
7 8 9

>> b = a(1:2,2:3) // this command gives selects the element of 1st row to 2nd row and
2nd column to 3rd column and displays the value as another
matrix ‘b’.
b=
2 3

13
Matrices

5 6

Q 4. Implement the syntax for adding ¿ adding column∧row .

Ans. a = [1 2;3 4] // this command creates 2X2 matrix of given values.


a=
7 2
3 4

>> b = [1 2] // this command creates 1X2 matrix of given values.


b=
1 2

>> c = [3;4] // this command creates 2X1 matrix of given values.


c=
3
4

>> d = [a c] // this command adds columns of two matrices.


d=
1 2 3
3 4 4

Q 5. Implement the syntax for displaying thediagonal of the¿ elements next ¿¿

Ans. a = [1 2 3;4 5 6; 7 8 9] // this command creates 3X3 matrix of given values.


a=
1 2 3
4 5 6
7 8 9

>> diag(a) // this command displays the values of diagonal of matrix a.


ans =
1
5
9

>> diag(a,1) // this command displays the values 1 step above the diagonal of matrix a
ans =
2
6

14
Matrices

Q 6. Implement the following syntax :eye ( m, n ) ;


zeros ( m , n ) ;
ones ( m, n ) ;
rand ( m ,n ) ;

Ans. eye(2,3) // this command creates a matrix with diagonal 1 and other elements 0.
ans =
1 0 0
0 1 0

>> zeros(2,2) // this command creates a ‘Zero’ 2X2 matrix.


ans =
0 0
0 0

>> ones(2,2) // this command creates a unity 2X2 matrix.


ans =
1 1
1 1

>> rand(2,2) // this command creates a random 2X2 matrix.


ans =
0.8147 0.6324
0.9058 0.0975

Q 7. Flip a ¿ ¿ ¿∧up ¿ downusing fliplr ∧flipud syntax .

>> a = [1 2 3;4 5 6; 7 8 9] // this command creates 3X3 matrix of given values.


a=
1 2 3
4 5 6
7 8 9

>> fliplr(a) // this command flips the given matrix from left to right.
ans =
3 2 1
6 5 4
9 8 7

>> flipud(a) // this command flips the given matrix from up to down.

15
Matrices

ans =
7 8 9
4 5 6
1 2 3

Q 8. Make lower ∧upper traingles of ¿

>> b = [1 2 3;4 5 6;7 8 9] // this command creates 3X3 matrix of given values.
b=
1 2 3
4 5 6
7 8 9

>> tril(b) // this command makes the upper triangle of given matrix ‘0’.
ans =
1 0 0
4 5 0
7 8 9
>> b = [1 2 3;4 5 6;7 8 9]; // this command creates 3X3 matrix of given values.
b=
1 2 3
4 5 6
7 8 9

>> triu(b) // this command makes the lower triangle of given matrix ‘0’
ans =
1 2 3
0 5 6
0 0 9

Q 9. Use the following syntax :linspace ; logspace nthroot

Ans. linspace(1,100,10) // this command creates an interval of 100 between 1 and 10.
ans =
Columns 1 through 9
5.0000 17.7778 30.5556 43.3333 56.1111 68.8889 81.6667 94.4444 107.2222
Column 10
120.0000

>> logspace(1,100,10) // this command creates an interval of 120 between 5 and 10 in log graph.
ans =
1.0e+100 *
Columns 1 through 10

0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000

16
Matrices

>> nthroot(36,6) // this command calculates the nth root of a given variable.
ans =
1.8171

17

You might also like