3

我有一个 2x2 矩阵,它的每个元素都是一个 1x5 向量。像这样的东西:

x = 1:5;
A = [ x  x.^2; x.^2 x];

现在我想找到行列式,但这发生了

B = det(A);
Error using det
Matrix must be square.

现在我明白为什么会发生这种情况了,MATLAB 将 A 视为 2x10 的双精度矩阵。我希望能够将 x 视为一个元素,而不是一个向量。我想要的是det(A) = x^2 - x^4,然后得到B = det(A)一个 1x5 向量。

我如何实现这一目标?

4

2 回答 2

3

While Matlab has symbolic facilities, they aren't great. Instead, you really want to vectorize your operation. This can be done in a loop, or you can use ARRAYFUN for the job. It sounds like ARRAYFUN would probably be easier for your problem.

The ARRAYFUN approach:

x = 1:5;
detFunc = @(x) det([ x x^2 ; x^2 x ]);

xDet = arrayfun(detFunc, x)

Which produces:

>> xDet = arrayfun(detFunc, x)
xDet =
     0   -12   -72  -240  -600

For a more complex determinant, like your 4x4 case, I would create a separate M-file for the actual function (instead of an anonymous function as I did above), and pass it to ARRAYFUN using a function handle:

xDet = arrayfun(@mFileFunc, x);
于 2012-07-19T17:00:09.577 回答
1

从数学上讲,行列式仅针对方阵定义。因此,除非您可以提供方阵,否则您将无法使用行列式。

注意我知道维基百科并不是所有资源。我只是提供它,因为我不能轻易地从我的大学微积分书中提供打印件。

更新:可能的解决方案?

x = zeros(2,2,5);
x(1,1,:) = 1:5;
x(1,2,:) = 5:-1:1;
x(2,1,:) = 5:-1:1;
x(2,2,:) = 1:5;

for(n=1:5)
    B(n) = det(x(:,:,n));
end

像这样的东西会起作用吗,或者您是否希望同时考虑每个向量?这种方法将每个“层”视为它自己的,但我有一个偷偷摸摸的怀疑,你想得到一个单一的值作为结果。

于 2012-07-19T15:44:42.147 回答