I will define a vector here as being a matrix with either a single column and many rows or a single row and many columns.
The following are two vectors
Now that we are settled on what a vector is, let’s look how to manipulate vectors using Matlab.
Vectors in Matlab
Create a vector in Matlab
How do you really create a vector in Matlab? We have learned lately how to create matrices and manipulate them in Matlab. Playing with vectors is similar. It goes like this:
If we would like the create the A and B vectors above, here is how we will go about it
A=[5,25,1,0,11,32]% The comma can be replace by a space B=[4;1;1;7;8]
Find the largest component of a vector
To find the largest element of A and B, use the following code
We can use this to find the largest element of a vector. Assume you have a vector with a thousand elements and can not go through each them to check which one is the largest, this function will take the pain off your shoulder.
Find the smallest component
As the preceding function, we would use
Find the length of a vector
The length of a vector will tell you the number of elements the vector has. To find the length of a vector, use
Sort in ascending order
Sum elements of a vector
The sum function will simply sum all elements of a vector
Find the mean value
The mean value formula is the following
This can also be written like this
To find the mean value of the vector A, use the following code
Find the standard deviation
To find the standard deviation, use the following code
Find the median value of a vector
The median of a vector is nothing but the number separating the higher half from the lower half. So the median of B with be 1, while the median of A will be 8.
To find the median value of the vector A, use the following code