Hi...friends this is second part of our last article which was MATLAB TUTORIAL 3- WORKING WITH MATRIX PART 1 .......lets continue
Colon operator
The colon operator
will prove very useful and
understanding how it
works is the key to efficient and convenient usage of MATLAB.
It occurs in several different forms.
Often
we must deal with matrices or vectors that
are too
large
to
enter one ele-
ment at a time.
For
example,
suppose we want to enter
a vector
x
consisting
of points (0, 0.1, 0.2, 0.3, · · · , 5). We can use the command
>> x = 0:0.1:5;
The row vector has 51 elements.
Linear spacing
On the other
hand,
there
is a command
to generate linearly spaced
vectors: linspace. It is similar
to the colon operator
(:), but gives direct
control over the number of points.
For
example,
y = linspace(a,b)
generates a row vector y of 100 points linearly spaced
between and including
a and b.
y = linspace(a,b,n)
generates
a row vector y of n points linearly
spaced between and including a and b. This is useful when we want to divide
an interval into a number of subintervals of the same length. For example,
>> theta = linspace(0,2*pi,101)
divides the interval [0, 2π] into 100 equal subintervals, then creating
a vector of 101 elements.
Colon operator in a matrix
The colon operator
can also be used to pick out a certain
row or column. For example,
the statement A(m:n,k:l specifies rows m to
n and column k to
l. Subscript
expressions refer
to portions of a matrix. For example,
>> A(2,:)
ans =
4 5 6
is the second
row elements of A.
The colon operator can also be used to extract a sub-matrix
from a matrix
A.
>> A(:,2:3)
ans
|
=
|
|
2
|
3
|
|
5
|
6
|
|
8
|
0
|
A(:,2:3) is a sub-matrix with the last two columns of A.
A row or a column
of a matrix can
be deleted by setting
it to a null vector, [ ].
>> A(:,2)=[]
ans
|
=
|
|
1
|
3
|
|
4
|
6
|
|
7
|
0
|
Deleting row or column
To delete a row or column
of a matrix, use the empty vector operator, [ ].
>> A(3,:) = [] A =
1 2 3
4 5 6
Third row of matrix
A is now deleted.
To restore the third row, we use a technique
for creating a matrix
>> A = [A(1,:);A(2,:);[7 8 0]]
A
|
=
|
||
1
|
2
|
3
|
|
4
|
5
|
6
|
|
7
|
8
|
0
|
Matrix A is now restored to its original
form.
Dimension
To determine
the dimensions of a matrix
or vector, use the command size. For example,
>> size(A)
ans =
3 3
means 3 rows and 3 columns.
Or more explicitly with,
>> [m,n]=size(A)
their are still some operations left so lets continue to PART3
No comments:
Post a Comment