I am going to be highlighting some of the built in functions that MATLAB has.
First of all, there are a bunch of common functions that are built into MATLAB.
Things like absolute value, square root, a signum function, which returns the sign of
a value, or an array, remainder, exponential, log is the natural log, log 2 is built in,
that's the base 2, log 10, and others, and what I wanted to do is highlight these round,
fixed, floor, and ceiling, because those are a little bit confusing.
First of all the difference between round and fix is that round always rounds it up
or down to the nearest integer, so anything that's 4.5 and above for example would go
up to 5, and anything below 4.5 would go down to 4.
Fixed just lops off any decimal places, and so the fix of 4.8 would be 4, so that's the
difference between round and fix.
Now we have floor and ceiling.
Floor rounds down to the integer that's below it, 4.8 would go down to 4.
Ceiling rounds up towards infinity, so ceiling of 4.8 is 5, and I can do the same on negative
numbers, so floor of -4.8 is -5, ceiling of -4.8 just rounds it up, which is -4, so there's
a difference between these four different functions.
Next we have the trig functions in MATLAB, and these are just the typical sin, cosine,
tangent, and so on.
There's one main difference, and one sort of enhancement that MATLAB has, and that is
we have sind(x), sind(x) does everything in degrees, so the inputs can be in degrees,
everything else is radians, so that's kind of nice that you don't have to convert degrees
to radians.
Next, we go into the array functions in MATLAB, so we have size(x), a lot of times size is
used in conjunction with a vector, I'll show you this in a moment, length(x), numel(x),
which is the number of elements.
We can generate different arrays of random numbers, and we also have rand(n), it generates
random numbers according to the Gaussian or normal distribution.
So for example if I did rand(5) that generates a 5x5 array of random numbers between 0 and
1.
rand(3,4) would generate a random array between 0 and 1, three rows, four columns, and randn(3,4)
would generate a 3x4 array of normally distributed random numbers, it has a mean 0 standard deviation
of 1.
I've created a new array now A, and I wanted to show you how we can use the size(x) function,
so size of A, that will return the rows and columns, but often times we want to actually
use the output of size(x) in some sort of iterations of calculations, so I could do
something like (nrows,ncols), that vector is going to be equal to the size of A.
We have actually stored the size of that into these two variables, nrow and ncols, because
that is described by the vector that's output from the size function.
Finally we have a couple more array functions in MATLAB, you can take the minimum of an
array, if you want to use that in conjunction with a vector [a,b] MATLAB will return the
location of that minimum in that array x, same thing with max(x) we can do mean, median
and mode, standard deviation, variance, can actually create a histogram, and then we've
got the sum(x), the prod(x), and we have various sort functions, so these sort functions I
wanted to highlight briefly because they are a point of confusion.
I've created a vector A which is just 1 through 10 in increments of 1.
Next I created a vector r, which is 10 random numbers.
I can create sort of an augmented array that's c, which is just a and r, and now I've declared
a new array, d, which is just sort(x) of c.
What sort(x) does is it independently sorts every column in ascending order, so you can
see that I've decoupled the different rows, so 7 is no longer with .2963.
I can also set up something like a sort(c), and if you put this 'descend' in there it
sorts everything descending.
Next I put in sortrows(c), and what that does is it sorts c depending upon the first column.
So it actually keeps the rows together, it doesn't independently sort the columns, it
keeps them together.
And then if I do sortrows(c,2), that will actually sort the rows by the second column,
and in a lot of cases you actually want to keep the rows together because you might have
corresponding data.
One last thing I want to show you is if you want to extract a column from an array, I've
reiterated my d array here, let's just say I want to pluck out the second column, you
can easily do that by just doing ar, this is a new vector, is equal to d, and I have
a colon here, 2.
Because the first index in an array is the rows colon just means all, so I have all rows
of column 2, and that is what results, and you will actually use these types of things
a lot when you're working with arrays.