返回行列式向量-Matlab [英] returning determinant vector - Matlab

查看:95
本文介绍了返回行列式向量-Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有3个维度Y(i,j,w)的矩阵. 我想获得行列式向量d(w),其中每个数字都是矩阵的行列式 Y(:,:,w).

I have a matrix with 3 dimentions Y(i,j,w). I want to get a determinant vector d(w), in which each number would be the determinant of the matrix Y(:,:,w).

它是否有优雅的语法,还是我只需要使用循环?

Is there an elegant syntax for it, or I just have to use a loop?

谢谢

推荐答案

首先,您几乎从不真正想要计算行列式,您只是以为自己就可以了.实际上,这几乎从来都不是一件好事,因为行列式的伸缩性很差.通常,它们被用来推断矩阵的奇异状态,这在数值分析方面是一件可怕的事情.

Well, first of all, you virtually NEVER truly want to compute a determinant, you just think you do. In fact, it is almost never a good thing, because determinants are so poorly scaled. Too often they are used to infer the singularity status of a matrix, which is a terrible thing to do in terms of numerical analysis.

说过我对决定因素的一般看法...

Having stated my mini-rant against determinants in general...

选项1:

将3维数组转换为正方形矩阵的单元格数组,并将该数组的每个平面作为一个单元格. mat2cell可以轻松高效地完成任务.

Convert your 3-d array into a cell array of square matrices, with each plane of the array as one cell. mat2cell will do the trick easily and efficiently.

接下来,在单元格数组上使用cellfun. cellfun可以对每个单元格应用一个函数(@det),然后将返回一个行列式向量.这效率极高吗?只要在编写循环时预先预先分配向量,与在循环中应用det相比,它可能不会带来太大收益.

Next, use cellfun on the cell array. cellfun can apply a function (@det) to every cell, and then will return a vector of determinants. Is this incredibly efficient? Its probably not a huge gain over applying det in a loop, as long as you pre-allocate the vector in advance when you write a loop.

选项2:

如果矩阵很小(例如2x2或3x3矩阵),则随着显式矢量乘法,展开行列式的乘法.我认为目前尚不清楚,因此对于2x2的情况,其中Y为2x2xn:

If the matrices are small, thus say 2x2 or 3x3 matrices, then expand out the multiplications for the determinant as explicit vector multiplies. I think this is not clear as I am writing it, so for a 2x2 case, where Y is 2x2xn:

d = Y(1,1,:).*Y(2,2,:) - Y(1,2,:).*Y(2,1,:);

您肯定会看到,它为矩阵Y的每个平面形成了一个2x2行列式的向量.3x3的情况非常简单,也可以写成六个项的三元乘积.我没有仔细检查下面的3x3外壳,但应该关闭.

Surely you see that this forms a vector of 2x2 determinants for every plane of the matrix Y. The 3x3 case is simple enough to write also, as six 3-way products of terms. I've not carefully checked the 3x3 case below, but it should be close.

d = Y(1,1,:).*Y(2,2,:).*Y(3,3,:) + ...
    Y(2,1,:).*Y(3,2,:).*Y(1,3,:) + ...
    Y(3,1,:).*Y(1,2,:).*Y(2,3,:) - ...
    Y(3,1,:).*Y(2,2,:).*Y(1,3,:) - ...
    Y(2,1,:).*Y(1,2,:).*Y(3,3,:) - ...
    Y(1,1,:).*Y(3,2,:).*Y(2,3,:);

如您所见,选项2将非常快,并且已向量化.

As you can see, OPTION 2 will be pretty fast, and it is vectorized.

作为对Chris的回应,所需时间存在显着差异.考虑一组1e5矩阵所需的时间.

as a response to Chris, there is a SIGNIFICANT difference in the time required. Consider the time required for a set of 1e5 matrices.

p = 2;
n = 1e5;
Y = rand(p,p,n);

tic,
d0 = squeeze(Y(1,1,:).*Y(2,2,:) - Y(2,1,:).*Y(1,2,:));
toc

Elapsed time is 0.002141 seconds.

tic,
X = squeeze(mat2cell(Y,p,p,ones(1,n)));
d1= cellfun(@det,X);
toc

Elapsed time is 12.041883 seconds.

这两个调用将相同的值返回到浮点垃圾中.

The two calls return the same values to within floating point trash.

std(d0-d1)
ans =
   3.8312e-17

循环不会更好,实际上肯定会更糟.因此,如果我要编写一段代码,以面对为数组中的许多这样的矩阵生成行列式的任务,我会特例编写2x2和3x3矩阵的代码.我什至可以将其写成4x4矩阵.是的,写出来是一团糟,但是所需的时间有很大差异.

A loop would be no better, in fact, surely worse. So were I to write a piece of code that would be faced with the task of generating determinants for MANY such matrices in an array, I would special case the code for 2x2 and 3x3 matrices. I might even write it out for 4x4 matrices. Yes, it is a mess to write out, but there is a big difference in the time required.

一个原因是MATLAB的det使用对LU的调用来分解矩阵.从理论上讲,这比中等大小的矩阵的乘积要好,但对于2x2或3x3的情况,额外的开销却是致命的. (我不会猜出收支平衡点在哪里,但是可以很容易地测试到这一点.)

One reason is that MATLAB's det uses a call to LU, factorizing the matrix. This is better in theory than the multiplies for even medium large matrices, but for a 2x2 or a 3x3, the extra overhead is a killer. (I won't guess where the break even point falls, but one could test that easily enough.)

这篇关于返回行列式向量-Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆