You are on page 1of 13

Generals

 Clear: It deletes all the variables from memory.


 Clc: clears the matlab command window but does not deletes the variables
 Whos: for listing of what variables have been created since the last applications of clear
 MATLAB permits the user to create variable names with a length of up to sixty three
alphanumeric characters, with the characters after the sixty-third being ignored.
Each variable name must start with either an uppercase or lowercase letter, which
can then be followed by any combination of uppercase and lowercase letters,
numbers, and the underscore character (_). No blank spaces may appear between
these characters. Variable names are case sensitive, so a variable named junk is
different from junK.
 There are, however, several variable names called keywords that are explicitly
reserved for MATLAB as part of its programming language.(break, return, switch, try,
….)
 The parentheses are the highest level in the hierarchy, followed by exponentiation,
then by multiplication and division, and finally by addition and subtraction. Within
each set of parentheses and within each level of hierarchy, MATLAB performs its

operations from left to right.


a
If is an array with each element in the array , use realsqrt(x) to increase computational
speed.
b
If is an array with each element in the array , use reallog(x) to increase computational
speed.
c
15 digits accuracy for ; for larger , only the magnitude and the 15 most significant digits
will be correct.

 Complex number : z = a +b *1j


 Abs(z); angle(z); complex(a,b); conj(z); imag(z); real(z)
Matrix Operations

Array Operation
1. Colon notation: x=s:d:f

s=start or initial value


d=decrement or increment value
f=final or end value

when d=1,simply, x=s:f


example:

>>x=1:2:11

x=

1 3 5 7 9 11

>>x=1:2:10

x=

1 3 5 7 9

Number of terms , n = length(x)

>> n = length(x)
n=

5
2.Creating equally spaced values: x=linspace(s,f,n)

d=(f-s)/(n-1)
for logarithmic scale: x=logspace(s,f,n)

>>x=logspace(1,7,7)

x=

10 100 1000 10000 100000 1000000 10000000

>>x=logspace(1,3,3)

x=

10 100 1000

>> sumx=sum(x)

sumn =

1110

3. Accessing element:

>>X=[2,3,5,6];
>>X(end)
=6

When we add or subtract a scalar from a vector ,then scalar is added or subtracted
from each element of vector.

>>Z=x-1
=[1,2,4,5]
>>Z(2)
=2

>>Y=[-1,6,15,-7,31,2,-4,-5];

>>x=[Y(1),Y(2),Y(7),Y(8)]

x=

-1 6 -4 -5

>>ind =[1,2,7,8];
>>x=Y(ind)
x=

-1 6 -4 -5

4. Sorting

[ynew,index]=sort(y,’ascend’)
[ynew,index]=sort(y,’descend’)

x=

-1 6 -4 -5

>> [xnew,index]=sort(x,'ascend')

xnew =

-5 -4 -1 6

index =

4 3 1 2

>> [xnew,index]=sort(x,'descend')

xnew =

6 -1 -4 -5

index =

2 1 3 4
5. find (Relation)

Index=find(x<=0)

>> x=[-1,2,-3,4,5,6,-7];
>> index=find(x<=0)

index =

1 3 7

>> z=x(index)

z=

-1 -3 -7

6. Maximum/Minimum

[xmin,locmin]=min(x)
[Xmax,locmax]=max(x)
>> x=[-1,2,-3,4,5,6,-7];

>> max(x)

ans =

>> min(x)

ans =

-7

>> [xmax,locmax]=max(x)

xmax =

6
locmax =

Matrix Operation

1. Creation of matrix:

>>a=[1,2,3;3,4,5;5,6,7]

a=

1 2 3
3 4 5
5 6 7

Using Ellipses (…):


>> a=[1,2,3;...
3,4,5;...
5,6,7]

a=

1 2 3
3 4 5
5 6 7

2. Order of matrix
[m,n]=size (a)
>>[m,n]=size (a)

m=
3
n=
3
3. Transpose: atr = a’ (apostrophe)

>> atr = a'

atr =

1 3 5
2 4 6
3 5 7

4. Special matrix

ones: matrix with all elements one.


>>A= ones(2,3)
A=
1 1 1
1 1 1

zeros: null matrix with all elements zero.


>>B= zeros(3,2)
B=
0 0
0 0
0 0

eye: formation of identity matrix


>>A=eye(3)
a=
1 0 0
0 1 0
0 0 1

diag: formation of diagonal matrix


>>a=[4,9,1];
>>A= diag(a) or diag([4,9,1])
A=

4 0 0
0 9 0
0 0 1
It also extracts diagonal elements from a given matrix.

>> a=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]

a=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

>> diag(a)

ans =

1
6
11
16

>> diag(a,1) %first upper off diagonal vector

ans =

2
7
12

>> diag (a,2) %second upper off diagonal vector

ans =

3
8

>> diag (a,-1) %first lower off diagonal vector

ans =

5
10
15

>> diag (a,-2) %second lower off diagonal vector

ans =

9
14

Magic: formation of magic square matrix

Z=magic(n≥3)
Z=magic(3)
z=

8 1 6
3 5 7
4 9 2

5.Min/Max in matrix

>>b=magic(4)
>>b=

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>>maxb=max(b)
maxb=

16 14 15 13 %gives maximum from all the column


>>minb=min(b)
minb =

4 2 3 1

>>max(max(b))
ans =

16

6. Matrix manipulation

A(2,:)=[ ] % deletes second row of matrix A


A(:,3:5)=[ ] %deletes the third through fifth column of matrix A
A ([1 3],:)=[ ] % deletes the first and third row of matrix A
u(5:length(u))=[ ] %deletes all the elements of vector u excepts 1 through 4.

7.Appending a row or column in a given matrix

A= [1,0,0;0,1,0;0,0,1]
U=[5 6 7]
V= [2;3;4]

A1=[A;U]
A2=[A V]
A3=[A u’]

8. Dot operations

Z1=X.*M
Z2=X./M
Z3=X.^M

>>X=1:8;
>>y=2.^X

y=

2 4 8 16 32 64 128 256

Or , x=linsolve(A,b)

8.Matrix operations:
det(A);inv(A);eig(A,B)
Anonymous Function and Symbolic Computation

1. fn_name=@(list of input variables) function_expression

>>f = @(x)x^3-3*x^2+x*log(x-1)+100
f=
@(x)x^3-3*x^2+x*log(x-1)+100

>> f(0)
ans =
100

>> f(1)
ans =
-Inf

>> values=[f(0) f(1) f(2) f(10)]


values =

100.0000 -Inf 96.0000 821.9722

>> x=[1,1, 2, 10];


>> f(x)
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.

Error in @(x)x^3-3*x^2+x*log(x-1)+100

>> f = @(x)x.^3-3*x.^2+x.*log(x-1)+100 % dot operation to vectorize the function

f=

@(x)x.^3-3*x.^2+x.*log(x-1)+100

>> f(x)

ans =

-Inf -Inf 96.0000 821.9722

You might also like