Matlab 3
Matlab 3
4 5 6
>> 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
>> a(2:3,:) = [ ] // this command selects the elements from row 2 to 3 and all
columns and make it null.
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(:,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
>> 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
>> diag(a,1) // this command displays the values 1 step above the diagonal of matrix a
ans =
2
6
14
Matrices
Ans. eye(2,3) // this command creates a matrix with diagonal 1 and other elements 0.
ans =
1 0 0
0 1 0
>> 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
>> 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
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