You are on page 1of 3

The colon operator(:).

MATLAB This is one of the most important operators in Matlab,


The expression,
Matrix manipulation 1:5 indicates a row matrix containing integers 1 2 3 4
A matrix is a rectangular array of numbers. 5.
To get non-unit spacing,
Entering matrices 10 : 2 : 20 yields 10 12 14 16 18 20
10 :-2 : 0 yields 10 8 6 4 2 0
123
A= 456 is represented by Homework 3
789 Obtain the following row matrix with colon operator.
A = [ 1 2 3; 4 5 6; 7 8 9 ] [2, (/2 + 2) ( + 2) ,(3/2 + 2)]
Note:
1.Separate the elements of a row with blanks commas If you use colon in subscript expressions you are
2.Separate the rows with semicolon (;) referring to some portion of a matrix. For instance,
3.Surround the list of elements with square brackets [ ] A(l :k,j) means 1st k elements in jth column of A.
A( :, j) means all elements along jth column of A.
If you want to add up all elements along each column, The colon(:) by itself refers to all elements in a row or
then use the sum command, Sum (A) column.
Note: If you do not specify the output variable matlab To refer to the last column of a matrix you can use the
uses the variable ans, which stands for answer to store keyword end.
the result of a calculation. A(:, end) means all elements in the last column.
If you want to swap two columns of matrix A you can
Transpose of a matrix do it as below.
Here you flip the matrix about the main diagonal and B = A (:, [3 2 1] )
ith row becomes ith column of the transpose matrix. Like other programming languages Matlab also gives
To take the transpose you can use an apostrophe or a mathematical expressions which involve
single quote with A; A' 1.Variables 2. Numbers 3. Operators 4. Functions

Homework 1: Variables
For the following matrix compute the summation of all Matlab can understand only first 31 characters of the
elements along each and every row separately. variable name. For the variable name, essentially you
123 have to start with a letter. Matlab is case sensitive
A= 456 hence it can distinguish between uppercase letters and
789 lowercase letters. For instance A and a are not the
same variable.
To get the diagonal elements of a matrix, diag (A)
To flip the matrix from left to right, fliplr (A) Numbers
Matlab uses conversional decimal notation. Scientific
Homework 2: notation uses the letter e to specify a power of ten.
For matrix A compute the summation of all elements Ex. 3 9.362 -99 0.002 3e-20 3e20
along the anti-diagonal. -9i 9j
Imaginary numbers use either i or j as a suffix for
Subscripts imaginary part.
To refer to the element in ith row and jth column you can Operators
use the notation A(i,j). + addition
for instance to add up some elements - subtraction
A(1,2) + A(2,3) + A(3,1) * multiplication
But the most elegant way of referring to an element is / division
to use a single subscript because Matlab will recognize ^ power
the matrix as just a column matrix, which has been ' complex conjugate transpose
formed by the columns of the original matrix. () specify evaluation order
A(3) Functions
If you want to include a value in an element outside the abs(x) - absolute value and complex magnitude
matrix you can do it in this way, sqrt(x) - square root
X = A; exp(x) - returns the exponential(ex)
X (1,4) = 10 sin(x) - sin value of angle x given in radian

1
Some special functions give the values of useful
constant
inf - infinity
NaN - Not a number
Pi - 3.14159
i or j - -1 [ ]
These functions are built in-functions. They are not
reserved. It is possible to overwrite any of them with a Deleting rows and columns
new variable. You can create your own functions with To delete 2nd column of A,
M-files. X=A;
Examples of expressions. X (:, 2) = [ ] then the answer,
nu_3 = 2 + abs(-5)

LR = ( 3 + sqrt(4)) /2 [ ]

Matrix Manipulation
Linear Algebra
Other ways of generating matrices.
Here we consider mathematical operations on matrices.
P = zeros (3,4) P = ones (2,3)
Matrix is a two-dimensional numeric array that
0000 111 represents a linear transformation.
0000 111 eg:-
0000 C= A+B means addition of two matrices.
C= det (A) means the determinant of A.
Q = 5*ones (2,3) C= inv (A) means the inverse of A.
555 C= eig (A) gives the eigen values of A.
555 C= A/5 means the division of A by a scalar.
C= A^2 means the 2nd power of A.
R = rand (3,3) uniformly distributed random elements To find the characteristic polynomial we can get it’s
Q = randn (3,3) normally distributed random elements coefficients in this way,
C= poly (A)
Load command Then we can write the characteristic polynomial since
If you save matlab files or text files containing numeric It gives the coefficients in the descending order of
data, under a name that ends in<.dat>. powers of  i.e. [n , n-1 , n-2…………., 0]
for eg: - Green.dat then the command,
load Green.dat will read the file and create the Arrays
variable Green containing the relevant documents. For arrays we do arithmetic operations element-by-
element. Matlab uses dot or decimal point to represent
M-files element-by-element operation.
You can create your own matrices with M- files. + Addition
- Subtraction
Homework 4 .* Element by element multiplication
Using some text editor create a file containing . / Element by element division
following matrices A&B and save it under a name that . ^ Element by element power
ends in <.m>. Then read the relevant file in Matlab and
take the output A+B. Array operations are useful in building tables
eg:-
[ ] [ ] A = (0:, 5)’;
LUM = [A A.^2 A.^3 ]

Concatenation
Concatenation is the process of joining small matrices
to form bigger ones.
Concatenation operator is a pair of square brackets [ ].
e.g:- [ ]
If B= [ A A+10 ; A+16 A+20 ]
Then the resulting matrix is a 6×6 matrix A=(1:0.2:2)’;
PEN = [A log10(A)]

2
2. if you want to save it in graphics format so that you
can use it in other applications also, select export in
the file menu. Then you will get a dialog box. Select
a suitable save as type and give a file name with an
extension.
[ ]
Polynomials
command line editing
Representing polynomials
 ctrl+p recall previous line
 ctrl+n recall next line In MATLAB, polynomials are represented as a row
 ctrl+b move back one character matrix containing coefficients ordered by descending
 ctrl+f move forward one character powers of the polynomials.
Home ctrl+a move to the beginning of the line For eg. p(x) = x2-5x+6 is represented by
End ctrl+e move to the end of the line
Esc ctrl+u clear the line P= [1 -5 6] and to the find roots, use the roots
Del ctrl+d delete the character at cursor command as given below.
Backspace ctrl+h delete the character before cursor Q= roots (p)

Basic plotting. The function poly returns the polynomial


To plot the variation of y versus x corresponding to coefficients.
x=0:0.2:1; S= poly (Q)
y=x+1
plot (x, y)
To label axes,
xlabel (‘x=no of people’)
ylable (‘y=no of seats available’) Homework 5
title (‘graph of variation of y with x’,’FontSize’, 16)
For the range of plot the following in a
Mulitple data sets in a single graph single graph with different colors (red, purple and
black) and markers (+, * and o) but with the same
Eg:- dotted line style.
x=0:0.2:1;
y1=x+1; y1=sin(x)
y2=x-1; y2=sin(x-0.2)
y3=x-2; y3=sin(x-0.5)
plot(x,y1,’r:+’,x,y2,’y,:*’,x,y3,’m:x’)
Do the following modifications.
to add a legend use the command 1. Name the x-axis as ‘x=0:2π’
legend(‘for x+1’,’for x-1’,’for x-2’) y-axis as ‘sin’
2. Give a title as ‘multiple data sets’ (font size16)
3. Give a legend as ‘for y1’
typical colors line styles markers ‘for y2’
‘for y3’ in order.
m-magenta - for solid +, *, o ,x 3. Add a text as ‘see carefully’ (font size 16) at
y-yellow _ for dashed the point (1,-0.4)
r-red : for dotted
g-green
b-blue Homework 6
w-white Find the roots of g(x) ; g(x)=x3 –x2 +2x+4
k-black

Homework 7
If the roots of a quadratic equation are 1+1.732i
and 1-1.1732i find the relevant polynomial.
saving a figure

1. Use save as in file menu.

You might also like