Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Chau Chemometrics From Basics to Wavelet Transform

.pdf
Скачиваний:
119
Добавлен:
15.08.2013
Размер:
2.71 Mб
Скачать

appendix

279

1.8000 -5.8914

2.0000 -6.7188

2.2000 -7.2967

2.4000 -7.4457

2.6000 -6.9406

2.8000 -5.5088

3.0000 -2.8345

One can also use the linspace function to produce linearly spaced vectors and the logspace function to generate logarithmically spaced vectors. For instance, linspace(x1, x2, N) will gives N linearly equally spaced points between x1 and x2.

If we key in the following statement

>linspace(.3,5.2,14)

the results obtained will be as follows:

ans=

Columns 1 through 7

0.3000

0.6769

1.0538

1.4308

1.8077

2.1846

2.5615

Columns 8 through

14

 

 

 

 

2.9385

3.3154

3.6923

4.0692

4.4462

4.8231

5.2000

MATLAB also provides functions to construct some special matrices. These include

diag: diagonal matrices and diagonals of a matrix. diag(V,K) where V is a vector with N components and K is an integer, the functions returns a square matrix of order N+abs(K) with the elements of V on the Kth diagonal. K=0 is the main diagonal, K > 0 is above the main diagonal; and K < 0 is below the main diagonal. If X is a matrix, diag(X) returns the main diagonal of X. Thus, diag(diag(X)) returns a diagonal matrix.

Hadamard: Hadamard matrix. hadamard(N) is a Hadamard matrix of order N , that is, a matrix, say, H, with elements 1 or −1 such that H H=N*IN (where IN denotes the identity matrix of order N ). A N- by-N (N × N ) Hadamard matrix with N > 2 exists only if N is divisible by 4, that is, rem(N,4)=0.

280

appendix

ones: ones array. ones(N) returns a matrix of order N with all the elements equals to one. ones(size(A)) gives a matrix of the same size as A with all the elements equal to one.

rand: uniformly distributed random numbers. rand(N) is an N-by- N matrix of random numbers that are uniformly distributed in the interval (0.0,1.0). rand with no argument returns a scalar, while rand(size(A)) is a matrix of the same size as A.

randn: normally distributed random numbers. randn(N) is an N-by-N matrix of random numbers that follow the normal distribution with mean zero and unity variance. randn(size(A)) returns a matrix of the same size as A.

eye: identity matrix. eye(N) returns an identity matrix of order N . eye(size(A)) is an identity matrix having the same size as A.

Thus, to produce a random matrix of order 3 × 5, one can simply key in the following statement:

>rand(3,5)

The results are as follows: ans=

0.9501

0.4860

0.4565

0.4447

0.9218

0.2311

0.8913

0.0185

0.6154

0.7382

0.6068

0.7621

0.8214

0.7919

0.1763

With these functions, we can easily construct data matrices of any size.

A.2.5. Matrix Subscript System

In order to indicate the position of an element in a matrix, subscripts are always used in mathematics. In principle, MATLAB follows the same rules as those of mathematics. There is no difference between MATLAB and other advanced computer languages. The only difference in MATLAB is that it is possible for MATLAB to use a vector subscript to define submatrix, through which MATLAB makes the matrix operation very convenient. For instance, if A is a matrix of order 10 × 10, the statement

>A(1:5,3)

can be used to construct a column vector of order 5 × 1, which consists of the first five elements in the third column in matrix A. Again, if we key in the statement

>X=A(1:5,7:10);

appendix

281

A

X

 

 

Figure A.11. Generation of a 5 × 4 submatrix using the command X=A(1:5,7:10) to specify the 20 elements (located in an upper right region with label X) within the 10 × 10 matrix A.

we can obtain a new matrix of X of order 5 ×4, which contains the elements in the last four columns and in the first five rows in matrix A as shown in Figure A.11, It should be noted that in this expression, if we use only a colon without specifying the starting and ending positions, the command embraces all the rows and/or all the columns of the matrix identified. For example, the statement

>A(:,3)

gives the third column in matrix A, while the command

>A(1:5,:)

gives the first five rows in matrix A.

The subscript expression of a matrix can be used in input statements, which makes the matrix operation in MATLAB very convenient. For instance, the following commands can be employed to construct two matrices, B and a:

>B=magic(8),

B =

64

2

3

61

60

6

7

57

9

55

54

12

13

51

50

16

17

47

46

20

21

43

42

24

40

26

27

37

36

30

31

33

32

34

35

29

28

38

39

25

41

23

22

44

45

19

18

48

49

15

14

52

53

11

10

56

8

58

59

5

4

62

63

1

282

 

 

appendix

 

 

 

>a=rand(8)

 

 

 

 

 

 

a =

 

 

 

 

 

 

 

0.9501

0.8214

0.9355

0.1389

0.4451

0.8381

0.3046

0.3784

0.2311

0.4447

0.9169

0.2028

0.9318

0.0196

0.1897

0.8600

0.6068

0.6154

0.4103

0.1987

0.4660

0.6813

0.1934

0.8537

0.4860

0.7919

0.8936

0.6038

0.4186

0.3795

0.6822

0.5936

0.8913

0.9218

0.0579

0.2722

0.8462

0.8318

0.3028

0.4966

0.7621

0.7382

0.3529

0.1988

0.5252

0.5028

0.5417

0.8998

0.4565

0.1763

0.8132

0.0153

0.2026

0.7095

0.1509

0.8216

0.0185

0.4057

0.0099

0.7468

0.6721

0.4289

0.6979

0.6449

We can replace part of the elements in the matrix a with that of B by the command

>a(:,[3 5 7])=B(:,1:3)

Then, the matrix a becomes

a =

0.9501 0.8214 64.0000 0.1389 2.0000 0.8381 3.0000 0.3784

0.2311 0.4447 9.0000 0.2028 55.0000 0.0196 54.0000 0.8600

0.6068 0.6154 17.0000 0.1987 47.0000 0.6813 46.0000 0.8537

0.4860 0.7919 40.0000 0.6038 26.0000 0.3795 27.0000 0.5936

0.8913 0.9218 32.0000 0.2722 34.0000 0.8318 35.0000 0.4966

0.7621 0.7382 41.0000 0.1988 23.0000 0.5028 22.0000 0.8998

0.4565 0.1763 49.0000 0.0153 15.0000 0.7095 14.0000 0.8216

0.0185 0.4057 8.0000 0.7468 58.0000 0.4289 59.0000 0.6449

This procedure replaced the third, fifth, and seventh columns of matrix a by the first three columns of matrix B.

In general, if v and w are integer vectors, then A(v,w) represents a submatrix originating from matrix A, in which the rows are determined by vector v, while the columns is determined by vector w. Here vector v is called a row subscript and w, the column subscript. In this way, some matrix calculations, which may be clumsy to program in other computer languages, can be easily implemented with the help of the subscript system in MATLAB.

Sometimes, we may need to vectorize a matrix before performing some calculations. This can be easily achieved in MATLAB through the following commands:

>A=[1 2 ; 3 4 ; 5 6] >b=A(:)

appendix

283

The following results can be obtained immediately:

A=

1 2

3 4

5 6 b=

1

3

5

2

4

6

The reshape function in MATLAB is another way to change the order of a matrix. For example, suppose that we want to change a matrix of order 3 × 4 into a matrix of order 2 × 6; this can be achieved by the following commands. First, we define a 3 × 4 matrix

>A=[1 4 7 10; 2 5 8 11; 3 6 9 12] A=

1

4

7

10

2

5

8

11

3

6

9

12

Then, the reshape function is used:

>B=reshape(A,2,6)

The result is

B=

1

3

5

7

9

11

2

4

6

8

10

12

It is worth noting that MATLAB also defines a special but important matrix, which is the empty matrix. An empty matrix can be constructed by the following statement:

>x=[ ]

In this way, x is an empty matrix and it can be used as a variable to do the calculation. Using this empty matrix as a variable, one can easily delete some rows and/or columns in a matrix:

>A(:,[2,4])=[]

The resulting matrix following this operation is that the submatrix of the second column and the fourth column in matrix A is deleted as follows:

284

appendix

>A(:,[2,4])=[]

>A=

1 7

2 8

3 9

In MATLAB, some MATLAB functions have their default values for the empty matrix. For instance, functions det (the determinant of a matrix), cond (conditioned number of a matrix), sum (sum of the elements in every column in a matrix), and others have their default values. If X is an empty matrix, then det(X)=1, cond(X)=0 and sum(X)=0. Note that an empty matrix is a very important variable in MATLAB programming.

In order to manipulate matrices easily, MATLAB provides many useful functions. Following are some examples:

max: largest component. For vectors, max(X) is the largest element in X. For matrices, max(X) is a row vector containing the maximum element from each column.

min: smallest component. For vectors, min(X) is the smallest element in X. For matrices, min(X) is a row vector containing the minimum element from each column.

mean: average or mean value. For vectors, mean(X) is the mean value of the elements in X. For matrices, mean(X) is a row vector containing the mean value of each column.

median: median value. For vectors, median(X) is the median value of the elements in X. For matrices, median(X) is a row vector containing the median value of each column.

std: standard deviation. For vectors, std(X) returns the standard deviation. For matrices, std(X) is a row vector containing the standard deviation of each column.

sum: sum of elements. For vectors, sum(X) is the sum of the elements of X. For matrices, sum(X) is a row vector with the sum over each column.

prod: product of elements. For vectors, prod(X) is the product of the elements of X. For matrices, prod(X) is a row vector with the product over each column.

sort: sort in ascending order. For vectors, sort(X) sorts the elements of X in ascending order. For matrices, sort(X) sorts each column of X in ascending order. When X is a cell array of strings, sort(X) sorts the strings in ASCII (American Standard Code for Information Interchange) dictionary order.

appendix

285

Please note that if an N-dimensional array X is passed as the argument, all the functions listed above operate on the first nonsingleton dimension of X.

conv: convolution and polynomial multiplication. C = conv(A, B) convolves vectors A and B. The resulting vector is length LENGTH(A)+ LENGTH(B)−1. If A and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials.

corrcoef: correlation coefficients. corrcoef(X) is a matrix of correlation coefficients formed from array X whose each row is an observation, and each column is a variable. corrcoef(X,Y), where X and Y are column vectors, is the same as corrcoef([X Y]).

Consider the following examples. First, let a matrix B be defined and the outputs of a few functions mentioned above be shown as

>B=[1 4 7 10 5; 2 5 8 11 4; 3 6 9 12 6; 2 5 7 3 2]

>B=

 

 

 

 

 

 

 

1

4

7

 

10

5

 

 

2

5

8

 

11

4

 

 

3

6

9

 

12

6

 

 

2

5

7

 

3

2

 

 

>max(B)

 

 

 

 

 

 

 

ans =

 

 

 

 

 

 

 

3

 

6

9

12

6

 

 

>min(B)

 

 

 

 

 

 

 

ans =

 

 

 

 

 

 

 

1

 

4

7

3

2

 

 

>mean(B)

 

 

 

 

 

 

 

ans =

 

 

 

 

 

 

 

2.0000

 

5.0000

7.7500

9.0000

4.2500

>prod(B)

 

 

 

 

 

 

 

ans =

 

 

 

 

 

 

 

12

 

600

 

3528

3960

240

 

286

 

 

 

appendix

>sort(B)

 

 

 

 

ans =

 

 

 

 

1

4

7

3

2

2

5

7

10

4

2

5

8

11

5

3

6

9

12

6

>corrcoef(B)

 

 

 

 

ans =

 

 

 

 

1.0000

1.0000

0.8528

0.2000

0.2390

1.0000

1.0000

0.8528

0.2000

0.2390

0.8528

0.8528

1.0000

0.6822

0.6625

0.2000

0.2000

0.6822

1.0000

0.9084

0.2390

0.2390

0.6625

0.9084

1.0000

The most attractive feature of the MATLAB language is that it provides very convenient functions for matrix operations. It makes some programs for processing chemical signals very easy and convenient to implement. Moreover, computations involving MATLAB script are usually very fast and efficient, significantly simplifying and facilitating chemometric programming in the MATLAB language.

A.2.6. Matrix Decomposition

Matrix decomposition is the core of chemometric techniques. Many algorithms used in chemometrics are based on matrix decomposition, such as principal-component analysis (PCA) and partial least squares (PLS). Some familiarity with the basic ideas of matrix decomposition will make it easier to follow the algorithms presented in this book.

A.2.6.1. Singular-Value Decomposition (SVD)

Singular-value decomposition is very important in chemometrics. In MATLAB, matrix decomposition can be performed simply by the following statement

>[U,S,V]=svd(A)

in which U is a column orthogonal matrix or so-called scores, matrix V is a row orthogonal matrix or so-called loadings, and matrix S is a diagonal

appendix

287

matrix satisfying the following relation:

A=U*S*V’

For example, assume that

>A=[1 4 7 10; 2 5 8 11; 3 6 9 12]

Then, we have

A=

1

4

7

10

2

5

8

11

3

6

9

12

Then, we can simply key in

>[U,S,V]=svd(A)

The following results are obtained:

U=

0.5045 0.7608 0.4082

0.5745 0.0571 -0.8165

0.6445 -0.6465 0.4082

S=

25.4624

0

0

0

0

1.2907

0

0

0

0

0.0000

0

V=

0.1409 -0.8247 0.1605 -0.5237

0.3439 -0.4263 0.1764 0.8179

0.5470 -0.0278 -0.8342 -0.0647

0.7501 0.3706 0.4973 -0.2295

A.2.6.2. Eigenvalues and Eigenvectors (eig)

Suppose that A is a square matrix. If a vector x and a scalar a satisfy theequation Ax=αx, then x and α are respectively called the eigenvector and eigenvalue of matrix A. In MATLAB, eigenvalues of A can be obtained by the following statement:

>e=eig(A)

Here e is a vector containing all the eigenvalues of A. If one also requires the corresponding eigenvectors, then the statement

>[V,D]=eig(A)

288

appendix

will produce a diagonal matrix D of eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that A*V=V*D. In addition, E=eig(A,B) is a vector containing the generalized eigenvalues of square matrices A and B. The statement [V,D]=eig(A,B) produces a diagonal matrix D of generalized eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that A*V=B*V*D. For example, if

>[x,d]=eig(A’*A)

then, we have

 

 

 

x=

 

 

 

0.5279

-0.1462

-0.8247

0.1409

-0.8128 -0.1986 -0.4263

0.3439

0.0419

0.8356

-0.0278

0.5470

0.2430

-0.4909

0.3706

0.7501

d=

 

 

 

-0.0000

0

0

0

0

0.0000

0

0

0

0

1.6658

0

0

0

0

648.3342

A.2.7. Graphic Functions

The MATLAB graphic functions are very powerful and extremely useful in generating scientific plots for data analysis, interpretation, and publication. These functions are very difficult to implement in most of the advanced computer languages without accessing other sophisticated graphic libraries. For instance, mesh plots and contour plots can be easily created in MATLAB. One simple statement can do the job. Here we give a very brief introduction on the graphics features of MATLAB.

We can begin by constructing a matrix of order 16 × 16 and then use this matrix data to show several powerful plotting functions in MATLAB. A small matrix is created first:

>A= [4

5

3

8

1

6

9

2

7

8

2

8

9

4

12

6]

A=

 

 

 

4

5

3

8

1

6

9

2

7

8

2

8

9

4

12

6

Соседние файлы в предмете Химия