Lecture3 MatrixFunctions
Lecture3 MatrixFunctions
max (x, y) Returns a matrix full of the larger of the two values in x and y.
m n (x) Determines the smallest value in x.
sum (x) Calculates the sum of elements in x.
prod (x) Calculates the product (multiplication) of elements in x.
Returns a vector with values of x, in ascending order (default).
sort (x)
2
Example : sum of vector
When the argument of sum funct on s a vector, then the
result w ll be a scalar (s ngle value).
>> sum(a)
ans =
150
>> sum(a, 2)
Totals of each row
ans =
(Parameter 2 means
60 roww se sum)
150
240 4
Example : cumsum Funct on
When argument s a vector, cumsum funct on returns a vector of
cumulat ve sums.
When argument s a matr x, funct on returns a vector of
cumulat ve sums for each column.
6
Example : mean Funct on
Mean funct on returns the average of elements n a vector
(or averages n a matr x).
ans =
21.6667
Examples w th Matr x
c = [1 3 7 ;
2 8 4 ;
6 -1 -2];
>> min(c)
ans = Returns m n mum of each column
1 -1 -2
>> mean(c)
ans = Returns average of each column
3.0000 3.3333 3.0000
8
Example : Matr x Sort ng
The default d mens on s columnw se, and the default order ng s the
ncreas ng order.
Sort ng does not mod fy the or g nal matr x.
It only generates a sorted new copy of the matr x.
>> c = [1 3 7 ;
2 8 4 ;
6 -1 -2];
10
Examples : Matr x sort ng
>> sort(c, 'descend')
>> c = [1 3 7 ; ans =
2 8 4 ; 6 8 7
6 -1 -2]; 2 3 4
1 -1 -2
Sorts each column separately n decreas ng order
>> sort(c, 2)
ans =
Sorts each row separately n ncreas ng order
1 3 7
(2 means roww se sort)
2 4 8
-2 -1 6
>> c = [1 3 7 ;
2 8 4 ;
6 -1 -2];
>> A=[10 20 30 40 ;
50 60 70 80 ;
90 100 110 120]
>> B = reshape(A, 6, 2)
B =
10 30
50 70
90 110
20 40
60 80
100 120
13
Wr te a Matlab program to read N (number of terms) from user,
then calculate Taylor ser es sum.
Test your program for X=150 degree.
Calculate the actual result (factual) by us ng the bu lt- n cos(X) funct on.
Calculate the truncat on error.
Absolute d fference between Taylor sum and the actual value s called the truncat on error.
Truncat on error = | factual ─ fsum |
14
Matlab Program
degree = 150;
X = degree*pi/180;
% Degree is converted to Radian
k = [0 : N]; % Vector
terim = (-1).^k .* X.^(2*k) ./ factorial(2*k) ;
fsum = sum( terim )
factual = cos(X)
truncation_error = abs(factual – fsum)
15
Screen outputs
fsum = fsum =
-0.9168 -0.8660
factual = factual =
-0.8660 -0.8660
truncation_error = truncation_error =
0.0508 0
16
Stat st cal Funct ons
Matlab
Descr pt on
Funct on
mean (x) Calculates the average of elements n vector x.
X (x x)
N N
2
N N
Ortalama =
68.7500
Varyans =
Screen 300.9167
output
Standart_dev =
17.3469
Mutlak_dev =
12.2500
18
Random number generat ng funct on: rand
Matlab
Descr pt on
Funct on
Generates nxn square matr x w th randomly
generated double (fract onal) data numbers.
rand (n) Range of generated random numbers are
between 0.0 to 1.0
Methods of
Read ng Data
from F les
20
Methods of Read ng Data from F les
21
3 4 20
The follow ng command reads all data into a 1 1 2
matrix, w th the same name of f le (c rcles). 7 5 10
>> load('circles.txt') 3 9 8
3 8 6
The follow ng command reads data into a matrix
that named d fferently (da reler). 4 9 6
>> daireler = load('circles.txt') 1 6 3
5 5 12
4 4 15
22
Access ng Matr x Columns
load ('circles.txt')
x = circles (: , 1) c rcles s a matr x, loaded from f le.
y = circles (: , 2) x, y, r are column vectors.
r = circles (: , 3)
23
24
Example Data F le : students.txt
Workspace 78
111 'AAA'
(Three column 85
222 'BBB'
vectors) 333 'CCC' 89
444 'DDD' 94
86
555 'EEE'
25
26
Import ng Data from
Spreadsheet F le
(M crosoft Excel, etc.)
nto MATLAB
27
28
Import ng CSV F le nto Matlab
• In Matlab, select the Current Folder.
• R ght-cl ck on the source f le name (c rcles.csv).
• Cl ck the Import Data command.
c rcles.csv F le
x;y;r
3;4;20
1;1;2
7;5;10
3;9;8
3;8;6
4;9;6
1;6;3
5;5;12
4;4;15
29
30
Import ng CSV F le nto Matlab
• After the mport ng, the Matlab Workspace conta ns the data values.
• Depend ng on the user select on, data w ll be n column vectors,
or n one matr x.
31