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

The code

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

max(A) max(B)

which returns

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

min(A) min(B)

### 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

length(A) length(B)

which returns

### Sort in ascending order

We have learned the use of this function in Matlab in detailed in this post.

### Sum elements of a vector

The **sum** function will simply sum all elements of a vector

The code

sum(A)

which returns

### 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

mean(A)

which returns

### Find the standard deviation

To find the standard deviation, use the following code

std(A)

which returns

### 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

median(A)

Which returns

### Content you might like:

Matlab Matrix Operations

Write a Matlab function that rotate a Matrix by 90 degrees

Matrix multiplication – Matlab

Matlab tricks: Creating an “array”, modifying matrix elements

Sort a matrix in Matlab

Vectors in Matlab: Basic Operations