Matrices are the basic data elements in MATLAB. This helps to make the code shorter and easier to understand as it reduces the number of loops and repeating statements. Single variables are treated as a matrix of a single element, also called scalars.
To declare a vector in MATLAB,
A= [10 34 29]; %row vector (1, 3)
Or
B = [114, 09, 46]; %row vector (1, 3)
Or
C= [101; 900; 20]; %column vector (3, 1)
Or
D= [1, 2, 3; 4, 5, 6; 7, 8, 9]; %3x3 Matrix
Matrix
Operations
Ø To
get the Transpose of a matrix, we replace the rows with columns and the columns
with rows. To get the transpose of a Matrix, we use ‘(apostrophe).
For example,
X1= [1, 2, 3];
X2=X1’
This command will
give X2 the value of [1; 2; 3]
X1= [1 2 3; 4 5 6;
7 8 9];
ð
X1 = 1 2 3
4 5 6
7 8 9
X2=X1’
ð
X2 = 1 4 7
2 5 8
3 6 9
Ø To
get the sum of all elements in a row/ column vector, we use a function called
sum(x).
For Example,
X1= [1, 2, 3];
A= sum(X1);
ð
A= 6
Ø To
get the mean of all elements in a row/column vector, we use a function called mean(x)
For example,
X1= [1, 2, 3];
A = mean(X1);
ð
A = 2
Ø To
get the product of elements in a row/column vector, we use a function called
prod(x)
For example,
X1 = [1, 2, 3];
A = prod(X1);
ð
A = 6
Ø To
get the minimum value in a row/column vector, we use a function min(x)
For example,
X1 = [1, 2, 3];
A = min(X1);
ð
A = 1
Ø To
get the maximum value in a row/column vector matrix, we use a function max(x)
For example,
X1 = [1, 2, 3];
A = max(X1);
ð
A = 3
Ø To
round off the elements to the nearest integers, we use round(x)
For example,
X1 = [1.1, 3.4,
5.6];
A = round(X1);
ð
A = [ 1, 3, 6]
Ø To
round off the elements to the nearest integer towards right (towards
+infinity), we use ceil(x)
For example,
X1 = [1.1, 3.4,
5.6];
A = ceil(X1);
ð
A = [ 2, 4, 6]
Ø To
round off the elements to the nearest integer towards left (towards -infinity),
we use floor(x)
For example,
X1 = [1.1, 3.4,
5.6];
A = floor(X1);
ð
A = [ 1, 3, 5]
Ø To
round off the elements to the nearest integer towards zero, we use fix(x)
For example,
X1 = [1.1, 3.4,
-7.2];
A = fix(X1);
ð
A = [ 1, 3, -7]
Ø To
sort elements in ascending or descending order, we use sort(x, ‘mode’). If the
mode is not specified, the matrix will get sorted in ascending order by
default.
For example,
X1 = [1, 6, 4, 2,
5];
A = sort(X1,
‘ascend’);
ð
A = [ 1, 2, 4, 5, 6]
B = sort(X1,
‘descend’);
ð
B = [ 6, 5, 4, 2, 1]
Ø To
determine the size of a matrix, we use size(x)
function.
For example,
[m, n ] = size(A);
Here, the number of
rows of matrix A will be assigned to ‘m’ and the number of columns of the
matrix A will be assigned to ‘n’.
Sub-matrices
and sub-vectors
Suppose we have a matrix A as defined below.
We want to create a sub-matrix of A.
A = 5 6 7
8 9 0
1 2 3
Now, if we give the command
B = A [ 2:3, 1:2 ];
ð
B = 8 9
1
2
C = A [ : , 1:2 ];
ð
C = 6 7
9 0
2 3
Generating
Special Matrices
The following MATLAB functions are used to
generate certain special types of matrices:
Ø zeros(a, b)
It returns an (a x b) matrix with all zeros.
For example,
M = zeros(2, 2);
M = 0 0
0 0
Ø ones(a, b)
It returns an (a x
b) matrix with all ones.
For example,
M = ones (3, 3);
M = 0 0 0
0 0 0
0 0 0
Ø eye(a, b)
Returns a matrix
with all diagonal elements equal to one.
For example,
M = eye(3 x 2);
M = 1 0
0
1
0
0
M
= eye(4 x 4);
M = 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Ø rand(a, b)
This command
generates an (a x b) matrix with random numbers in the interval (0, 1)
M = rand(3, 3);
M = 0.7898 0.4847 0.8879
0.5656 0.4623 0.6745
0.9482 0.9928 0.1234
Ø rands(a, b)
This command is
similar to rand(), it generates an (a x b) matrix with random elements in the
range (-1, 1).
For example,
M = rands(2, 3);
M = -0.2324 -0.3534
0.4534 0.5526
0.2342 -0.3424
This sums up all the basics about vectors and
matrices in MATLAB. The next post will be about Polynomials in MATLAB. Any
doubts/queries will be attended to in the comments section.
0 comments:
Post a Comment