MATLAB TUTORIAL - PART VII
Element-by-element operations are applied on each element of the array. As we saw earlier, addition and subtraction are already E-B-E. However, multiplication, division and exponentiation are not E-B-E but by adding the dot ‘.’ before the operator, it will behave as E-B-E. Of course in this case, matrices should be of the same size.
E-B-E operations are very useful when we have a function F(x), and we want to get the value of F(x) for several values of x. Below is an example showing how to use E-B-E:
>> X=1:6
X =
1 2 3 4 5 6
>> F=X.^2 -3.*X
F =
-2 -2 0 4 10 18
>> Y=cos(X)
Y =
0.5403 -0.4161 -0.9900 -0.6536 0.2837 0.9602
Since from now and on we will be dealing with array, it is a good idea to have a look at the functions used to analyze arrays:
- mean(M) returns the mean of a vector or array (for each column)
- max(M) , min(M) returns the max (min) of a vector M, and the max(min) of each column when M is a matrix
- [p n]=max(V) , min(V) returns the max(min) element in V, and n is the position of this element. If it happens that this max(min) is different positions, it returns the first position
- sum(V) returns the sum of elements in vector V
- sort(V) arranges the elements of vector V in ascending order
- median(V) returns the median value of the elements of V
- std(V) returns the standard deviation of the elements of V
- det(M) returns the determinant of matrix M
- dot(A,B) returns the dot product of A and B (scalar value)
- cross(A,B) returns the cross product of A and B
rand command is used to generate uniformly distributed numbers, whose values are between 0 and 1.
It can be used to assign random values to:
- scalar >> X=rand;
- vector>>V=rand(1,n) or rand(n,1)
- matrix>>M=rand(m,n) or rand(n) for a square matrix
We have also randperm(n) which generates a row vector with n elements that are a random permutation of integers 1 through n.
If we need random numbers belonging to a certain interval (a,b) we can use:
(b-a)* rand(m,n) +a
And if we want to have integers, we can round the above value using the command round.
We have also randn which is the same as rand but numbers are normally distributed with mean 0 and standard deviation 1. And if we want to change the mean and standard deviation, we can use the following command:
sd*randn + M
where sd is the desired standard deviation and M is the desired mean.
In the next tutorial, we will see how to evaluate polynomial, use symbolic math, and plotting. Click below to see the next post.
Filed under: MATLAB

Leave a Reply
You must be logged in to post a comment.