Spring til indhold
Hub Nexus
Opdateret

ForfatterIngen forfatter endnuOvertag den

Ser du noget at forbedre? Foreslå en ændring.

Støtte

Denne side er offentlig.

Operation on Vectors

Constructing Vectors

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

V e c t o r = ( f i r s {t}_{n} u m b e {r}_{i} {n}_{t} h {e}_{m} a t r i x : i n c r e m e n t a {l}_{v} a l u e : l a s {t}_{n} u m b e {r}_{i} {n}_{t} h {e}_{v} e c t o r )

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

A = [ A 1 , A 2 , A 3 , ... , A n ]

B = [ B 1 , B 2 , B 3 , ... , B n ]

A . B = A 1. B 1 + A 2. B 2 + A 3. B 3 + ... + A n . B n

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

Where this page came from

This page was imported from Wikibooks. From “MATLAB Programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

I disse publikationerMATLAB Programming

SprogEnglish

Licens: CC BY-SA 4.0 · Bearbejdet efter en.wikibooks.org

Visninger

Kommentarer

Spinner Logo
Version: 2CC0 1.0 (offentligt eje)
The runaway star that left the Tarantula Nebula
Version: 2CC0 1.0 (offentligt eje)
The Eagle Nebula, seen in the infrared
Version: 2CC0 1.0 (offentligt eje)
The Cosmic Cliffs are not cliffs
Version: 2CC0 1.0 (offentligt eje)
The recommended targets are real, but the largest health gain is between doing nothing and doing a little. That is the part worth knowing if you are starting from nothing.
Version: 2CC0 1.0 (offentligt eje)
Sleep is not one state but a cycle through several, each doing different work. Most sleep disorders are a failure to complete that cycle rather than a shortage of hours.
Version: 2CC0 1.0 (offentligt eje)
Four nebulae that revealed the shape of our galaxy