Tutorial:JMathLab/9 Vectors

From HandWiki

Vectors

Vector types are either used for multidimensional objects, or for simultaneous calculations on large numbers of data, e.g. for statistical problems. In this chapter we discuss this latter aspect.

Vectors are marked with square brackets. The elements are entered as comma-separated list. The commas may be left if the elements can be distinguished in a unique manner:

<jc lang="math"> x=[1 -2 3 -4]; printf('%f\n',x); printf('%f',length(x)); </jc>

Colon and the function line space are used to define ranges of numbers as vectors.

<jc lang="math"> y=1:10;  % 1 to 10, step 1 printf('%f',y) </jc>

with the outputs: <jc lang="math"> y = [ 1 2 3 4 5 6 7 8 9 10 ] printf('%f',y) </jc>

Vectors are often used to achieve fairly complex data manipulation effects. "Colon notation" (which is used both to generate vectors) and subscripting by vectors are keys to efficient manipulation of these objects. Creative use of these features permits one to minimize the use of loops and to make code simple and readable. Special effort should be made to become familiar with them.

The expression 1:5 (met earlier in for statements) is actually the row vector [1 2 3 4 5]. The numbers need not be integers nor the increment one. For example, 0.2:0.2:1.2 gives [0.2, 0.4, 0.6, 0.8, 1.0, 1.2], and 5:-1:1 gives [5 4 3 2 1]. The following statements will, for example, generate a table of sines. Try it.

<jc lang="math"> x = [0.0:0.1:2.0]; y = sin(x); printf('x=%f\n',x); printf('y=%f\n',y); </jc> Note that since sin operates entry-wise, it produces a vector y from the vector x.

<jc lang="math"> y=1:0.1:1.5  % 1 to 1.5, step 0.1 y = [ 1 1.1 1.2 1.3 1.4 1.5 ] y=linspace(0,2,5)  % 5 from 0 to 2.5, equidistant. printf('%f',y) </jc>

The number of elements in a vector x is calculated with the function length(x), individual elements are extracted by providing the index k like x(k). This index k must be a number in the range 1 to (including) length(x). The colon operator plays a special role: Used as index, all elements of the vector are returned. Additionally, ranges of numbers can be used as index.

Below we replace the print statement with "ans": <jc lang="math"> y = [ 1 2 3 4 5 6 7 8 9 10 ] a=y(2); printf('%f\n',a) % single element a=y(:); printf('%f\n',a) % magic colon a=y(2:3); printf('%f\n',a) % index between 2 and 3 a=y(2:length(y)); printf('%f\n',a)  % all from index 2 a=y([1,3,4]); printf('%f\n',a)  % indices 1,3 and 4 a=y([1,3,4]) = [1,2,3]  % insert printf('%f\n',a) </jc>

Working with vectors

Working with vectors is not too different from work with matrices. For example, let us create a vector with values equal 1:

<jc lang="math"> y=ones(1,10) printf('%f',y) </jc>

If you want to have all elements to be equal 5, simply multiply this vector by 5: Here we create a vector of size 10 with identical values 5:

<jc lang="math"> y=ones(1,10) * 5 printf('%f',y) </jc>

If we need to make a vector with 0 values, use zeros: <jc lang="math"> y=zeros(1,10) printf('%f',y) </jc>

Example

Calculate

[math]\displaystyle{ \sum_{k=1}^{k=n}k, \sum_{k=1}^{k=n}k^2, \sum_{k=1}^{k=n}k^3 }[/math]

for n=10, n=100, n=10000. The solution is given below:

<jc lang="math"> n=10; k=1:n; a1=sum(k); a2=sum(k.*k); a3=sum(k.^3) printf('%f',a1) </jc>

<jc lang="math"> n=100; k=1:n; a1=sum(k); printf('%f\n',a1) a2=sum(k.*k) printf('%f\n',a2) a3=sum(k.^3) printf('%f\n',a3) </jc>