sum,transpose 和 diag

和,转置和特征值

你可能已经发觉,一个魔方的相加它的元素的各种多种方式特的殊性质。如果你要相加单独任意行或列,或单独或两个主要对角线,你将总会得到相同的数值。让我们使用MATLAB来验证。首先陈述:

sum(A)

MATLAB响应:

ans =
    34    34    34    34

但你指定一个输出变量,MATLAB使用这个变量回复,简短回答,存储一个计算的结果。你已经计算了一个行向量包含A的列的和,每行有相同的和,神奇的和,34。

如何求取行的和?MATLAB有一个矩阵的列计算优先权,,因此最容易的方式去获得这个行的和是转置这个矩阵,计算转置的列的和,然后转置结果。转置操作是表示通过一个单引号,“ ' ”。它转置一个矩阵关于它的主要对角线和它的一个行向量转变到一个列向量。

因此

A'

产生

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

然后

sum(A')'

产生一个列向量含有行向量的和

ans =
    34
    34
    34
    34

在主要对角线上的元素和。对角线是同sum和diag函数得到。

diag(A)

产生

ans =
    16
    10
     7
     1

然后

sum(diag(A))

产生

ans =
    34

其他对角线,这样调用antidiagonal,不是非常重要数学,因此MATLAB没有准备现成的函数。但是一个函数最初打算在图形中使用,从左向右翻转一个矩阵。

sum(diag(fliplr(A)))
ans =
    34

你验证了在Dürer's版画中的矩阵的确是一个魔方并且,在这个过程中,极少有MATLAB矩阵运算示例。

sum, transpose, and diag

You are probably already aware that the special properties of a magic square have to do with the various ways of summing its elements. If you take the sum along any row or column, or along either of the two main diagonals, you will always get the same number. Let us verify that using MATLAB. The first statement to try is

sum(A)

MATLAB replies with

ans =
    34    34    34    34

When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of a calculation. You have computed a row vector containing the sums of the columns of A. Sure enough, each of the columns has the same sum, the magic sum, 34.

How about the row sums? MATLAB has a preference for working with the columns of a matrix, so the easiest way to get the row sums is to transpose the matrix, compute the column sums of the transpose, and then transpose the result. The transpose operation is denoted by an apostrophe or single quote, '. It flips a matrix about its main diagonal and it turns a row vector into a column vector.

So

A'

produces

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

And

sum(A')'

produces a column vector containing the row sums

ans =
    34
    34
    34
    34

The sum of the elements on the main diagonal is obtained with the sum and the diag functions.

diag(A)

produces

ans =
    16
    10
     7
     1

and

sum(diag(A))

produces

ans =
    34

The other diagonal, the so-called antidiagonal, is not so important mathematically, so MATLAB does not have a ready-made function for it. But a function originally intended for use in graphics, fliplr, flips a matrix from left to right.

sum(diag(fliplr(A)))
ans =
    34

You have verified that the matrix in Dürer's engraving is indeed a magic square and, in the process, have sampled a few MATLAB matrix operations. The following sections continue to use this matrix to illustrate additional MATLAB capabilities.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值