Lec-10 Manipulations of Matlab Matrices

advertisement
Lec-10
Manipulations of Matlab Matrices
Built-in functions
• The chapter will be covered quickly
• There are quite a few built-in functions in MATLAB
– If you can think of it, it is probably available
– To find documentation, use Google to find
documentation:
e.g. google “sine matlab”
google “standard deviation matlab”
Nesting of functions
• Just in regular math where one can use f(g(x))
• Arguments can be scalars, vectors or matrices
Parameters (Arguments)
Parameter and argument are almost synonyms
sqrt(x) is a function with one parameter
If we call sqrt as sqrt(4)
4 is the argument passed to the parameter
sin(x) is a function of one parameter too
If we call it as sin(sqrt(4))
It’s the value of sqrt(4) is passed as the argument
to its one parameter.
Functions with multiple arguments
• We have encountered linspace(1,10,100)
• rand(3,4)
• rem(12.1,5.0)
ans=
2.1
Matlab help
• You can use MATLAB help
• If you don’t know the name, use Google.
• Try “help” tab in MATLAB window.
More MATLAB Functions
Rounding Functions
Trigonometric Functions





sin(x) sine of x expressed in radians)
cos(x), tan(x)
sind(x) sine of x in degrees
cosd(x)
tand(x) etc.
Data Analysis Functions








max (maximum)
min (minimum)
mean (average)
median
sum (sum total of elements)
prod (product)
sort (sort elements)
cumsum, cumprod (cummulative sum, prod)
mean
sum and cumsum
Example of mean with vector output
If you can imagine it, it is likely there
Sorting
Try
• Try out at least 10 of the functions I just described with
both scalar and vector inputs
– At least one of fix, round, ceil
– At least one of the trig functions in radians and
degrees.
– Some of the sum, prod, cumsum
– Try log of exp
– Try max with vector output
• Compute the average location of the maximum
values from the columns of the matrix magic(5)
– Sort the rows of magic(5) according to the third column
Problem
Problem 3.20:
• Throw one die
• Throw two dice
• Throw two dice 100 times along with the total
of each throw
• Histogram the sum
Problem
>> one=floor(rand(1)*6+1)
one =
5
>> two=floor(rand(1,2)*6+1)
two =
2 4
>> sim=floor(rand(2,10).*6+1)
sim =
1 2 5 3 2 1 4 1 1 1
4 4 5 1 6 5 6 3 6 5
>> total=sum(sim)
total =
5 6 10 4 8 6 10 4 7 6
>> hist(total);
Problem
Problem
Download