Professional Documents
Culture Documents
>> a = 10
a =
10
>> b = 20;
>>
Special MATLAB Variables
>> 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
>> 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.
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
>> 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
Exercise 3: Find the average of the even numbers between 60 and 128.
>> mean([60:2:128])
ans =
94
>> x = sum([25:-5:5]/2.4)
x =
31.2500