You are on page 1of 15

Access elements in a matrix or a vector 1 2 3

>> A(2,[1 2 3]) A  4 5 6


>> b = A(2,3)
b = ans = 7 8 9
6 4 5 6
>> A(2,[1:3])
>> A(3,3) ans =
ans = 4 5 6
9 >> c = A([2 3],2)
>> ans * 2 c =
ans = 5
18 8
>> d = A([1 3],[2 3])
>> A(:,3) d =
ans = 2 3
3 8 9
6 >> A([2 3],:)
9 ans =
>> A(3,:) 4 5 6
ans = 7 8 9
7 8 9
Suppressing Output with Semicolon
MATLAB will print the result of every assignment operation unless the
expression on the right hand side is terminated with a semicolon.

>> a = 10
a =
10
>> b = 20;
>>
Special MATLAB Variables

beep generate sound


>> beep
pi  >> 1/0
ans =
inf (e.g. 1/0) Inf
I j ( 1 )
>> pi
ans =
3.1416

>> i
ans =
0+ 1.0000i
Arithmetic Operators

* Multiplication

/ Right division

\ Left division

^ Power

' Transpose
>> a = 8 >> e=[1 2 3 ; 7 8 9; 12 14 16]
a = e =
8 1 2 3
7 8 9
>> b = 2 12 14 16
b =
2 >> f = e'
f =
>> c=a/b 1 7 12
c = 2 8 14
4 3 9 16

>> d=a\b >> g = [6:9]’


d = g =
0.2500 6
7
8
9
Performing Arithmetic Operations on Matrices

• Adding and Subtracting Scalar to Matrices


>> a = [1 2 3;4 5 6;7 8 9] >> b = a-2
a = b =
1 2 3 -1 0 1
4 5 6 2 3 4
7 8 9 5 6 7

>> a+3
ans =
4 5 6
7 8 9
10 11 12
• Multiplication and Division Matrices by Scalars
>> a = [1 2 3;4 5 6;7 8 9] >>d = 2
a = d =
1 2 3 2
4 5 6 >> c= a/d
7 8 9 c =
0.5000 1.0000 1.5000
>> b = a*3 2.0000 2.5000 3.0000
b = 3.5000 4.0000 4.5000
3 6 9
12 15 18
21 24 27
Built-in Functions (Commands)
• Functions used for scalars and operate element-wise when applied to
a matrix or vector.

Sample of Built-in Functions


sin cos tan atan asin
log abs sqrt round floor
std var

At any time the command help can be used to get help from MATLAB
>> tan(0.3) >> a=[pi 1.5*pi 15.67 -22]
ans = a =
0.3093 3.1416 4.7124 15.6700 -22.0000
>> cos(pi) >> cos(a)
ans = ans =
-1 -1.0000 -0.0000 -0.9993 -1.0000
>> abs(-8) >> abs(a)
ans = ans =
8 3.1416 4.7124 15.6700 22.0000
>> sqrt(9) >> sqrt(abs(a))
ans = ans =
3 1.7725 2.1708 3.9585 4.6904
>> round(15.6) >> round(a)
ans = ans =
16 3 5 16 -22
>> floor(15.6) >> floor (a)
ans = ans =
15 3 4 15 -22
New commands and functions
sum(A)
• If A is a vector, then sum(A) returns the sum of the elements.
• If A is a matrix, then sum(A) returns a row vector whose elements
are the sums of each column.
• If A is an empty 0-by-0 matrix, then sum(A) returns 0.

mean(A)
• f A is a vector, then mean(A) returns the mean of the elements.
• If A is a matrix, then mean(A) returns a row vector whose elements
are the mean of each column.
• If A is an empty 0-by-0 matrix, then mean(A) returns NaN.

linspace(a,b,n)
• generates a row vector of n points linearly spaced between and
including a and b.
>> a = [2:5:22]
a =
2 7 12 17 22
>> b = [1 5 3; 3 7 6; 4 6 8; 4 5 7]
b =
1 5 3
3 7 6
4 6 8
4 5 7

>> w = sum(a)
w =
60

>> s = sum(b)
s =
12 23 24

>> mean([7; 9; -3; 2.7; 5.2])


ans =
4.1800
>> mean(b)
ans =
3.0000 5.7500 6.0000

>> linspace(4,20,5)
ans =
4 8 12 16 20

>> linspace(0,5,11)
ans =
Columns 1 through 5
0 0.5000 1.0000 1.5000 2.0000
Columns 6 through 10
2.5000 3.0000 3.5000 4.0000 4.5000
Column 11
5.0000
Exercise1: Construct an array of 100 elements between 0 and 2.

>> x = [0:2*pi/99:2*pi];

>> y = linspace(0,(2*pi),100);

>> whos
Name Size Bytes Class Attributes

x 1x100 800 double


y 1x100 800 double
Exercise 2: Find the summation of the numbers 5 to13.
>> a=[5:13]
a =
5 6 7 8 9 10 11 12 13
>> sum(a)
ans =
81

Exercise 3: Find the average of the even numbers between 60 and 128.
>> mean([60:2:128])
ans =
94

Exercise 4: Find the value of x from the following series.

>> x = sum([25:-5:5]/2.4)
x =
31.2500

You might also like