更多关于矩阵和队列

来源:互联网 发布:网络理财诈骗案 编辑:程序博客网 时间:2024/04/19 22:58

更多关于矩阵和队列

这部分展示了更多关于矩阵和队列的操作,焦点:

(1)线性代数

(2)队列

(3)多元数据

(4)标量扩展

(5)逻辑下标

(6)find函数

一、线性代数

随便,术语矩阵和队列经常被交替使用。更多准确的说,一个矩阵是一个二维数字队列,其代表一个线性变换。数学操作符定义在矩阵上的线性代数的主题。

Dürer的魔方

A = [16     3     2    13
      5    10    11     8
      9     6     7    12
      4    15    14     1 ]

提供几个例子,其一个MATLAB矩阵操作附的测试。你已经看到矩阵转置,A'。添加一个矩阵到它的转置得到一个对称矩阵。

A + A'

ans =
    32     8    11    17
     8    20    17    23
    11    17    14    26
    17    23    26     2

乘法符号,*,表示矩阵乘法涉及到居于行和列之间的内乘。一个矩阵的转置被原型矩阵做乘法也产生一个对称矩阵。

A'*A

ans =
   378   212   206   360
   212   370   368   206
   206   368   370   212
   360   206   212   378

这个细节矩阵的行列式碰巧是零,表明这个矩阵是单数。

d = det(A)

d =
     0

减少A的行梯次形式不是恒等的。

R = rref(A)

R =
     1     0     0     1
     0     1     0    -3
     0     0     1     3
     0     0     0     0

自从矩阵是单数,它不会有逆矩阵。如果你尽力去计算逆矩阵则

X = inv(A)

你将会得到一个警告消息

Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 9.796086e-018.< /FONT >

警告:矩阵紧密到单数或错误的尺寸。

         结果可能被疏阔。RCOND = 9.796086e-018。< /FONT >

从检测强制异常中了解到环绕脱离错误已经阻止矩阵逆运算。但是rcond的值,其基于对等条件揣度,在eps的命令上,浮点亲属精度,因此计算逆矩阵拥有着诸多用处。

魔方的特征值是很有兴趣的。

e = eig(A)

e =
   34.0000
    8.0000
    0.0000
   -8.0000

一旦特征值是零,其其他特性的后果。最大的特征值是34,魔方的和。那是因为所有1的向量是一个特征值。

v = ones(4,1)

v =
     1
     1
     1
     1

A*v

ans =
    34
    34
    34
    34

当一个魔方被它的魔方和除。

P = A/34

结果是一个双精度随机矩阵,它的行和列的和全为1.

P =
    0.4706    0.0882    0.0588    0.3824
    0.1471    0.2941    0.3235    0.2353
    0.2647    0.1765    0.2059    0.3529
    0.1176    0.4412    0.4118    0.0294

例如矩阵在一个Markov过程中代表变化率。矩阵的重复指数代表过程的重复阶段,例如我们的例子,fifth幂

P^5

    0.2507    0.2495    0.2494    0.2504
    0.2497    0.2501    0.2502    0.2500
    0.2500    0.2498    0.2499    0.2503
    0.2496    0.2506    0.2505    0.2493

这个展示了作为k途径无限,所有元素在第k次幂, Pk次幂,途径1/4。

最终,在特征多项式中的系数

poly(A)

     1   -34   -64  2176     0

表明特征多项式

常数术语是零,因为矩阵是单数,并且四方的系数是-34,因为矩阵是魔方!

二、队列

当它们夺取线性代数的世界,矩阵变为二维数值队列。在队列上运算操作符的一个元素被一个元素。这个是加和键的含义是与矩阵和队列相同的,但是乘法操作符是不同的。MATLAB使用一个点,或小数点,作为乘法队列操作符符号的一部分。

操作符的列表包含:

 + 加 - 减 * 元素被元素乘法 ./ 元素被元素除法 .\ 元素被元素左除 .^ 元素被元素幂 .' 非变化队列转置

如果Dürer 魔方是被它自己乘,作为一个队列乘法

A.*A

结果是一个队列含有含有整数1到16的平方,在一个通用的命令中。

ans =
   256     9     4   169
    25   100   121    64
    81    36    49   144
    16   225   196     1

组建表格

队列操作符Shiite通用组建表格。假设n是列向量

n = (0:9)';

然后

pows = [n  n.^2  2.^n]

组建一个方形并且2的幂的表格

pows =
     0     0     1
     1     1     2
     2     4     4
     3     9     8
     4    16    16
     5    25    32
     6    36    64
     7    49   128
     8    64   256
     9    81   512

元素数学函数操作符在队列上元素被元素。因此

format short g
x = (1:0.1:2)';
logs = [x log10(x)]

组建一个对数的表格

 logs =
      1.0            0
      1.1      0.04139
      1.2      0.07918
      1.3      0.11394
      1.4      0.14613
      1.5      0.17609
      1.6      0.20412
      1.7      0.23045
      1.8      0.25527
      1.9      0.27875
      2.0      0.30103

 

三、多元数据

MATLAB使用列命令运算为多元统计数据。每列在数据中设置代表一个变量和每行一个观察。第(i,j)各元素是第j个变量的第i个观察。

作为一个例子,考虑一个带有三各变量的数据设置:

(1)心率

(2)体重

(3)每星期运动的时长

五个观察,结果队列可以像

D = [ 72          134          3.2
      81          201          3.5
      69          156          7.1
      82          148          2.4
      75          170          1.2 ]

第一行包含病人1的心率,体重和运动时长,第二行包含数据为病人2的,等等。现在你能够应用许多MATLAB数据计算函数去调整这个数据。例如得到平均值和每列的标准差。

mu = mean(D), sigma = std(D) < /FONT >

mu =
      75.8       161.8         3.48

sigma =
    5.6303      25.499       2.2107

在MATLAB中,数据计算函数的列表,键入

help datafun

如果你有存取到统计工具箱,键入

help stats

四、标量扩展

在几个不同的方式中,矩阵和标量能够被联合。例如,矩阵中的每个元素能够被一个元素减。在我们的魔方中元素的平均值是8.5,因此

B = A - 8.5< /FONT >

一个矩阵它的列向和是零。

B =
      7.5     -5.5     -6.5      4.5
     -3.5      1.5      2.5     -0.5
      0.5     -2.5     -1.5      3.5
     -4.5      6.5      5.5     -7.5

sum(B)

ans =
     0     0     0     0

标量扩展,MATLAB在一个范围中分配一个特定标量到所有索引位置。例如,

B(1:2,2:3) = 0< /FONT >

零输出到B的一部分。

B =
      7.5        0        0      4.5
     -3.5        0        0     -0.5
      0.5     -2.5     -1.5      3.5
     -4.5      6.5      5.5     -7.5

五、逻辑下标

逻辑向量从逻辑和关系操作符中被创建,能够被使用到参考附属队列中。假设X是一个普通矩阵并且L是一个相同尺寸的矩阵,其相同逻辑操作符的结果。然后X(L)特定X的元素,L的元素位置是非零的。

这种下标在一个阶段中能够被作为下标表示规定为逻辑操作符。假设你有以下数据设置。

x = [2.1 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8];< /FONT >

非数是一个错过观察的标记,例如在一个调查表中一个项目的一个失败的反应。带有逻辑索引位置删除错过的数据,使用isfinite(x),其是对的为有限的数值而错误为非数和inf。

x = x(isfinite(x))
x =
  2.1 1.7 1.6 1.5 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8

现在那里是一个观察,5.1,其似乎非常不同于其他的。它是一个局外人。下面语句删除局外人,从意义上讲,在这个事件中那些元素超过三个标准差。

x = x(abs(x-mean(x)) < = 3*std(x))
x =
  2.1 1.7 1.6 1.5 1.9 1.8 1.5 1.8 1.4 2.2 1.6 1.8

其他例子,在Dürer的魔方中素数值的逻辑被使用逻辑索引位置和标量扩展到设置非素数到0所强调。(查看魔方函数)

A(~isprime(A)) = 0< /FONT >

A =
     0     3     2    13
     5     0    11     0
     0     0     7     0
     0     0     0     0

六、find函数

find函数确定队列元素的索引位置,其面临一个给定逻辑条件。在它的最简约的形式中,查找返回一个索引的列向量。转置列向量,得到一个索引的行向量。例如,再次开始Dürer的魔方 。(查看magic函数)

k = find(isprime(A))'< /FONT >

使用一维索引摘出在魔方中的素数。

k =
     2     5     9    10    11    13

显示那些素数,作为一个在命令中被k确定的行向量,

A(k)

ans =
     5     3     2    11     7    13

在一个任务语句中当你使用k作为一个左手边索引时。矩阵结构是被保存的。

A(k) = NaN< /FONT >

A =
    16   NaN   NaN   NaN
   NaN    10   NaN     8
     9     6   NaN    12
     4    15    14     1

 

More About Matrices and Arrays

This section shows you more about working with matrices and arrays, focusing on

(1)Linear Algebra

(2)Arrays

(3)Multivariate Data

(4)Scalar Expansion

(5)Logical Subscripting

(6)The find Function

一、Linear Algebra

Informally, the terms matrix and array are often used interchangeably. More precisely, a matrix is a two-dimensional numeric array that represents a linear transformation. The mathematical operations defined on matrices are the subject of linear algebra.

Dürer's magic square

A = [16     3     2    13
      5    10    11     8
      9     6     7    12
      4    15    14     1 ]

provides several examples that give a taste of MATLAB matrix operations. You have already seen the matrix transpose, A'. Adding a matrix to its transpose produces a symmetric matrix.

A + A'

ans =
    32     8    11    17
     8    20    17    23
    11    17    14    26
    17    23    26     2

The multiplication symbol, *, denotes the matrix multiplication involving inner products between rows and columns. Multiplying the transpose of a matrix by the original matrix also produces a symmetric matrix.

A'*A

ans =
   378   212   206   360
   212   370   368   206
   206   368   370   212
   360   206   212   378

The determinant of this particular matrix happens to be zero, indicating that the matrix is singular.

d = det(A)

d =
     0

The reduced row echelon form of A is not the identity.

R = rref(A)

R =
     1     0     0     1
     0     1     0    -3
     0     0     1     3
     0     0     0     0

Since the matrix is singular, it does not have an inverse. If you try to compute the inverse with

X = inv(A)

you will get a warning message

Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 9.796086e-018.

Roundoff error has prevented the matrix inversion algorithm from detecting exact singularity. But the value of rcond, which stands for reciprocal condition estimate, is on the order of eps, the floating-point relative precision, so the computed inverse is unlikely to be of much use.

The eigenvalues of the magic square are interesting.

e = eig(A)

e =
   34.0000
    8.0000
    0.0000
   -8.0000

One of the eigenvalues is zero, which is another consequence of singularity. The largest eigenvalue is 34, the magic sum. That is because the vector of all ones is an eigenvector.

v = ones(4,1)

v =
     1
     1
     1
     1

A*v

ans =
    34
    34
    34
    34

When a magic square is scaled by its magic sum,

P = A/34

the result is a doubly stochastic matrix whose row and column sums are all 1.

P =
    0.4706    0.0882    0.0588    0.3824
    0.1471    0.2941    0.3235    0.2353
    0.2647    0.1765    0.2059    0.3529
    0.1176    0.4412    0.4118    0.0294

Such matrices represent the transition probabilities in a Markov process. Repeated powers of the matrix represent repeated steps of the process. For our example, the fifth power

P^5

is

    0.2507    0.2495    0.2494    0.2504
    0.2497    0.2501    0.2502    0.2500
    0.2500    0.2498    0.2499    0.2503
    0.2496    0.2506    0.2505    0.2493

This shows that as approaches infinity, all the elements in theth power,, approach.

Finally, the coefficients in the characteristic polynomial

poly(A)

are

     1   -34   -64  2176     0

This indicates that the characteristic polynomial

is

The constant term is zero, because the matrix is singular, and the coefficient of the cubic term is -34, because the matrix is magic!

二、Arrays

When they are taken away from the world of linear algebra, matrices become two-dimensional numeric arrays. Arithmetic operations on arrays are done element by element. This means that addition and subtraction are the same for arrays and matrices, but that multiplicative operations are different. MATLAB uses a dot, or decimal point, as part of the notation for multiplicative array operations.

The list of operators includes

 + Addition - Subtraction .* Element-by-element multiplication ./ Element-by-element division .\ Element-by-element left division .^ Element-by-element power .' Unconjugated array transpose

If the Dürer magic square is multiplied by itself with array multiplication

A.*A

the result is an array containing the squares of the integers from 1 to 16, in an unusual order.

ans =
   256     9     4   169
    25   100   121    64
    81    36    49   144
    16   225   196     1

Building Tables

Array operations are useful for building tables. Suppose n is the column vector

n = (0:9)';

Then

pows = [n  n.^2  2.^n]

builds a table of squares and powers of 2.

pows =
     0     0     1
     1     1     2
     2     4     4
     3     9     8
     4    16    16
     5    25    32
     6    36    64
     7    49   128
     8    64   256
     9    81   512

The elementary math functions operate on arrays element by element. So

format short g
x = (1:0.1:2)';
logs = [x log10(x)]

builds a table of logarithms.

 logs =
      1.0            0
      1.1      0.04139
      1.2      0.07918
      1.3      0.11394
      1.4      0.14613
      1.5      0.17609
      1.6      0.20412
      1.7      0.23045
      1.8      0.25527
      1.9      0.27875
      2.0      0.30103

三、Multivariate Data

MATLAB uses column-oriented analysis for multivariate statistical data. Each column in a data set represents a variable and each row an observation. The (i,j)th element is the ith observation of the jth variable.

As an example, consider a data set with three variables:

(1)Heart rate

(2)Weight

(3)Hours of exercise per week

For five observations, the resulting array might look like

D = [ 72          134          3.2
      81          201          3.5
      69          156          7.1
      82          148          2.4
      75          170          1.2 ]

The first row contains the heart rate, weight, and exercise hours for patient 1, the second row contains the data for patient 2, and so on. Now you can apply many MATLAB data analysis functions to this data set. For example, to obtain the mean and standard deviation of each column

mu = mean(D), sigma = std(D)

mu =
      75.8       161.8         3.48

sigma =
    5.6303      25.499       2.2107

 

For a list of the data analysis functions available in MATLAB, type

help datafun

If you have access to the Statistics Toolbox, type

help stats

四、Scalar Expansion

Matrices and scalars can be combined in several different ways. For example, a scalar is subtracted from a matrix by subtracting it from each element. The average value of the elements in our magic square is 8.5, so

B = A - 8.5

forms a matrix whose column sums are zero.

B =
      7.5     -5.5     -6.5      4.5
     -3.5      1.5      2.5     -0.5
      0.5     -2.5     -1.5      3.5
     -4.5      6.5      5.5     -7.5

sum(B)

ans =
     0     0     0     0

With scalar expansion, MATLAB assigns a specified scalar to all indices in a range. For example,

B(1:2,2:3) = 0

zeroes out a portion of B.

B =
      7.5        0        0      4.5
     -3.5        0        0     -0.5
      0.5     -2.5     -1.5      3.5
     -4.5      6.5      5.5     -7.5

五、Logical Subscripting

The logical vectors created from logical and relational operations can be used to reference subarrays. Suppose X is an ordinary matrix and L is a matrix of the same size that is the result of some logical operation. Then X(L) specifies the elements of X where the elements of L are nonzero.

This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression. Suppose you have the following set of data.

x = [2.1 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8];

The NaN is a marker for a missing observation, such as a failure to respond to an item on a questionnaire. To remove the missing data with logical indexing, use isfinite(x), which is true for all finite numerical values and false for NaN and Inf.

x = x(isfinite(x))
x =
  2.1 1.7 1.6 1.5 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8

Now there is one observation, 5.1, which seems to be very different from the others. It is an outlier. The following statement removes outliers, in this case those elements more than three standard deviations from the mean.

x = x(abs(x-mean(x)) <= 3*std(x))
x =
  2.1 1.7 1.6 1.5 1.9 1.8 1.5 1.8 1.4 2.2 1.6 1.8

For another example, highlight the location of the prime numbers in Dürer's magic square by using logical indexing and scalar expansion to set the nonprimes to 0. (See The magic Function.)

A(~isprime(A)) = 0

A =
     0     3     2    13
     5     0    11     0
     0     0     7     0
     0     0     0     0

六、The find Function

The find function determines the indices of array elements that meet a given logical condition. In its simplest form, find returns a column vector of indices. Transpose that vector to obtain a row vector of indices. For example, start again with Dürer's magic square. (See The magic Function.)

k = find(isprime(A))'

picks out the locations, using one-dimensional indexing, of the primes in the magic square.

k =
     2     5     9    10    11    13

Display those primes, as a row vector in the order determined by k, with

A(k)

ans =
     5     3     2    11     7    13

When you use k as a left-hand-side index in an assignment statement, the matrix structure is preserved.

A(k) = NaN

A =
    16   NaN   NaN   NaN
   NaN    10   NaN     8
     9     6   NaN    12
     4    15    14     1

原创粉丝点击