# MATLAB Programming/Vector and Matrices/Operations on Vectors

### Operation on Vectors

#### Constructing Vectors

If a vector's elements is following the sequential additions, we can just use parentheses with the following:

${\displaystyle Vector=(first_{n}umber_{i}n_{t}he_{m}atrix:incremental_{v}alue:last_{n}umber_{i}n_{t}he_{v}ector)}$
As for example, if we want to have a vector starting with number 1 and ends with number 19 with the incremental values of 3, we can type commands shown below

```>> V = (1:3:19)

V =
1     4     7    10    13    16    19
```

#### Accessing Vector

To access the values in vector , you just need to specify the vector and the array index, from 1 to n .

Using example of vector V above, we access single elements of vector V in third elements which we type commands as followed:

```>> V(3)

ans =
7
```

We can access multiple elements by typing the start index and the end index For example, we need to access element no 3 up to element no 6, we type as followed:

```>> V(3:6)

ans =
7    10    13    16
```

#### Finding Length

We can also use length function to knows what is the length of the vector ,for example we can know what is the length of V. There are 7 elements in vector V, therefore the answer should be 7

```>> length(V)

ans =
7
```

#### Sum Vector

We can find the sums of vector using sum function, it get this values by adding all of the numbers inside vector together: For example, 1+4+7+10+13+16+19

```>> sum(V)

ans =
70
```

#### Finding Max and Min

Respectively we can know which is the biggest and smallest number in a vector which we can use by using min and max functions

```>> min(V)

ans =
1

>> max(V)

ans =
19
```

#### Finding Mean

The mean function is to find the center/middle value inside the vector

```>> mean(V)

ans =
10
```

#### Finding Average

The average function is to find the average of the vector

```>> average(V)

ans =
10

>> % Another way of finding average
>> sum(V)/length(V)

ans =
10
```

#### Finding Standard Deviation

To find standard deviation , std function is needed
The standard deviation is a measure of the amount of variation or dispersion of a set of values. A low standard deviation indicates that the values tend to be close to the mean (also called the expected value) of the set, while a high standard deviation indicates that the values are spread out over a wider range.

```>> std(V)

ans =
6.4807
```

#### Random Permutation

There are two different ways to create random permutations for vector

a. randperm(N) returns a vector containing a random permutation of the integers 1:N.

```>> P=randperm(7)

ans =
1     6     2     3     4     5     7
```

b. randperm(N,K) returns a row vector containing K unique integers selected randomly from 1:N

```>>>> P=randperm(7,5)

P =
5     1     6     2     3
```

#### Sorting Vector

You may want to sort the vectors, you can do this MATLAB.

If the vectors are to be sorted in descending manner, use this example as followed. Note, we use the random permutation example b above.

```>> % using argument 'descend' to sort from big to small number
>> S=sort(P,'descend')

S =
6     5     3     2     1
```
```>> % using argument 'asscend' to sort from  small to big number
>> S=sort(P,'ascend')

S =
1     2     3     5     6
```

#### Calculating dot products

To find dot product,you can use the dot functions

NOTE: The two vectors must have the same length. The answer will usually in the forms of scalar .

The dot product of vector are calculated as followed

${\displaystyle A=[A1,A2,A3,...,An]}$

${\displaystyle B=[B1,B2,B3,...,Bn]}$

${\displaystyle A.B=A1.B1+A2.B2+A3.B3+...+An.Bn}$

```>> % creating row vector A and B
>> A = [3 ,7 , 2]

A =
3     7     2

>> B = [1,2,5]

B =
1     2     5

>> dot(A,B)

ans =
27
```

#### Calculating cross products

To find cross product,you can use the cross functions NOTE: The two vectors must have the same length of 3. The answer will usually in the forms of vector 3 .

TODO: Search a picture that can illustrate the cross product well.

```>> cross(A,B)

ans =
31   -13    -1
```

#### Calculating commonly used values in vector

To find the most duplicated values in the vector, we can use the mode functions.

```>>%create vectors with random numbers
>> A=randi(50,1,10)

A =
18    42    30    28    46    15    38    38    20    29

>> mode(A)

ans =
38
>>%38 appear twice in that vector
```