Matlab逆操作和警告

时间:2012-10-13 22:25:45

标签: matlab warnings linear-algebra matrix-inverse

不太清楚这意味着什么。 “警告:Matrix的工作精度非常高。”

我有一个名为matrix bestM的3x4矩阵 矩阵Q是bestM的3x3,矩阵m是bestM的最后一列

我想做C = - Q *矩阵m的逆矩阵 我收到了警告 和C = [Inf Inf Inf]这是不对的,因为我正在计算世界上的相机中心

bestM = [-0.0031 -0.0002 0.0005 0.9788;
         -0.0003 -0.0006 0.0028 0.2047;
         -0.0000 -0.0000 0.0000 0.0013];   

Q = bestM(1:3,1:3);
m = bestM(:,4);

X = inv(Q);
C = -X*m;
disp(C);

2 个答案:

答案 0 :(得分:5)

奇异矩阵可以被认为是等于零的矩阵,当你试图反转0时它会爆炸(变为无穷大),这就是你在这里得到的。用户1281385使用format命令提高精度是绝对错误的; format命令用于更改显示给您的格式。事实上,格式的帮助命令的第一行是

  

格式不会影响MATLAB计算的完成方式。

答案 1 :(得分:1)

如找到here,奇异矩阵是没有逆矩阵的矩阵。正如dvreed77已经指出的那样,您可以将其视为矩阵的1/0。

为什么我回答,是告诉你明确地使用inv几乎不是一个好主意。如果您需要相同的倒数几百次,那么它可能是值得的,但是,在大多数情况下,您对产品C感兴趣:

C = -inv(Q)*m

可以使用反斜杠运算符在Matlab中更准确,更快地计算:

C = -Q\m

输入help slash以获取更多相关信息。即使你碰巧发现自己确实需要明确的反转,我仍然建议你避免inv

invQ = Q\eye(size(Q))

下面是一个小小的性能测试,用于演示极少数情况下显式反转可以派上用场的方法之一:

% This test will demonstrate the one case I ever encountered where
% an explicit inverse proved useful. Unfortunately, I cannot disclose
% the full details without breaking the law, but roughly, it came down
% to this: The (large) design matrix A, a result of a few hundred 
% co-registrated images, needed to be used to solve several thousands 
% of systems, where the result matrices b came from processing the 
% images one-by-one. 
%
% That means the same design matrix was re-used thousands of times, to 
% solve thousands of systems at a time. To add to the fun, the images
% were also complex-valued, but I'll leave that one out of consideration
% for now :)  

clear; clc

% parameters for this demo
its = 1e2;
sz  = 2e3;
Bsz = 2e2;

% initialize design matrix
A = rand(sz);

% initialize cell-array to prevent allocating memory from consuming 
% unfair amounts of time in the first loop. 
% Also, initialize them, NOT copy them (as in D=C,E=D), because Matlab 
% follows a lazy copy-on-write scheme, which would influence the results
C = {cellfun(@(~) zeros(sz,Bsz), cell(its,1), 'uni', false)   zeros(its,1)};
D = {cellfun(@(~) zeros(sz,Bsz), cell(its,1), 'uni', false)   zeros(its,1)};
E = {cellfun(@(~) zeros(sz,Bsz), cell(its,1), 'uni', false)   zeros(its,1)};

% The impact of rand() is the same in both loops, so it has no 
% effect, it just gives a longer total run time. Still, we do the 
% rand explicitly to *include* the indexing operation in the test. 
% Also, caching will most definitely influence the results, because 
% any compiler (JIT), even without optimizations, might recognize the 
% easy performance gain when the code computes the same array over and 
% over again. It probably will, but we have no control over when and 
% wherethat happens. So, we prevent that from happening at all, by 
% re-initializing b at every iteration. 

% The assignment to cell is a necessary part of the demonstration; 
% it is the desired output of the whole calculation. Assigning to cell 
% instead of overwriting 'ans' takes some time, which is to be included 
% in the demonstration, again for cache reasons: the extra time is now
% guaranteed to be equal in both loops, so it really does not matter -- 
% only the total run time will be affected.

% Direct computation
start = tic;
for ii = 1:its    
    b = rand(sz,Bsz);    
    C{ii,1} = A\b;
    C{ii,2} = max(max(abs( A*C{ii,1}-b )));
end
time0 = toc(start);
[max([C{:,2}])   mean([C{:,2}])   std([C{:,2}])]

% LU factorization (everyone's
start = tic;
[L,U,P] = lu(A, 'vector');
for ii = 1:its    
    b = rand(sz,Bsz);    
    D{ii,1} = U\(L\b(P,:));
    D{ii,2} = max(max(abs( A*D{ii,1}-b )));
end
time1 = toc(start);
[max([D{:,2}])   mean([D{:,2}])   std([D{:,2}])]


% explicit inv
start = tic;
invA = A\eye(size(A)); % NOTE: DON'T EVER USE INV()!
for ii = 1:its
    b = rand(sz,Bsz);
    E{ii,1} = invA*b;
    E{ii,2} = max(max(abs( A*E{ii,1}-b )));
end
time2 = toc(start);
[max([E{:,2}])   mean([E{:,2}])   std([E{:,2}])]

speedup0_1 = (time0/time1-1)*100
speedup1_2 = (time1/time2-1)*100
speedup0_2 = (time0/time2-1)*100

结果:

% |Ax-b|
1.0e-12 * % max.   mean     st.dev.
          0.1121   0.0764   0.0159 % A\b   
          0.1167   0.0784   0.0183 % U\(L\b(P,;))
          0.0968   0.0845   0.0078 % invA*b

speedup0_1 = 352.57 % percent
speedup1_2 =  12.86 % percent
speedup0_2 = 410.80 % percent

应该很清楚,明确的逆有其用途,但就像任何语言的goto构造一样 - 谨慎而明智地使用