简体   繁体   中英

If A.B = C, given A,B,C are matrices, how do I calculate A from B and C using Matlab?

A, B and C are matrices.

A*B = C

Now I want to do a reverse ie calculate A using B and C. How do I do it? Matlab says that B should be a square matrices to calculate its inverse.

IF a unique solution exists, then one might best use pinv to find it. Using the example posed by schwarz...

A = [2 3 4];
B = [11 11 11; 12 12 12; 13 13 13];
C = A*B;

Ahat = C*pinv(B)
Ahat =
        2.788       3.0415       3.2949

The problem is, B is singular. So there are potentially infinitely many solutions.

B = magic(3)
B =
     8     1     6
     3     5     7
     4     9     2
A = [2 3 4];
C = A*B
C =
    41    53    41

Ahat = C*pinv(B)
Ahat =
            2            3            4

Ahat = C/B
Ahat =
     2     3     4

See that pinv and slash both yield the same solution, since B is non-singular AND it is well conditioned.

But how about if we try something that is less well conditioned? In this next xample, I'll use a matrix that is not really that bad.

>> A = [2 3 4];
>> B = [1 1 1;1 2 3;2 3 4.00001]
B =
    1              1              1
    1              2              3
    2              3        4.00001

Well, it has a pretty large condition number, but this matrix is not what I'd call numerically singular.

cond(B)
ans =
           2865128.4655819

C = A*B
C =
                        13                        20                  27.00004

Lets try several different solutions now.

format long g
Ahat1 = C*pinv(B)
Ahat1 =
     2     3     4

pinv did quite well.

Ahat2 = C/B
Ahat2 =
          2.00000000017764          3.00000000017764          3.99999999982236

Ahat3 = C*inv(B)
Ahat3 =
          1.99999999953434          2.99999999953434          4.00000000046566

slash and inv were both not bad, although clearly worse in this case. The pinv solution seems a bit more stable for this problem.

We might as well throw a QR factorization at this too. Use a pivoted solution for the best stability. Note that when your system is nearly singular, we will still expect problems.

[Q,R,P] = qr(B);

You can see the problem by inspecting R. The last diagonal element is tiny compared to the remainder. This will cause problems in the solution, amplifying any noise.

R
R =
         -5.09902735824196         -2.35339392337313         -3.72620671848107
                         0         0.679365175314723         0.339681455393392
                         0                         0     -2.88675134520189e-06

The QR factors have the property that Q*R*P' = B. So we can solve for A here as:

Ahat4 = ((C*P)/R)*Q'
Ahat4 =
          2.00000000076851           3.0000000007685           3.9999999992315

Note that I've arranged the parens to be as efficient as possible, since MATLAB will use the property of R as a triangular matrix to simply do a backsolve. We don't want MATLAB to be factorizing a matrix that is already factored.

But now lets look at one posed by vahid:

Ahat5 = C*B'*(inv(B*B'))
Ahat5 =
              1.9970703125               2.998046875              4.0029296875

However, the solution posed by vahid was simply terrible. Do NOT use this last form. PLEASE. There is a reason why people tell you not to do so, or they should have told you that! Yes, I know there are a group of people who do not know the mathematics involved, and they keep spreading it. You may even find it in some uninformed textbooks.

The nice thing about pinv is it works for any matrix, singular or not. If a solution exists, it will find one. If the solution is unique, it will work. If the solution is not unique, then well, what do you expect?

You can use the backslash operator:

% if A*C = B
C = A\B

I don't think the solution in case of non-rectangular A is unique though...

First multiply two sides of A*B=C by B' (transpose of B):

A*B*B'=C*B'

Let D=B*B' (D is a square matrix):

A*D=C*B'

Now multiply two sides of above equation by inv(D) :

A*D*inv(D)=C*B'*inv(D)

D*inv(D)=I , so:

A=C*B'*inv(D)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM