# Chemometrics from basick to wavelet transform - Chau F.T

ISBN 0-471-20242-8

**Download**(direct link)

**:**

**99**> 100 101 102 103 104 105 .. 112 >> Next

>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; 56] >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 x 4 into a matrix of order 2 x 6; this can be achieved by the following commands. First, we define a 3 x 4 matrix

>A=[1 4 7 10; 2 5 8 11; 369 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.

**99**> 100 101 102 103 104 105 .. 112 >> Next