How to use mldivide ("\") instead of inv in this formulae

74 views (last 30 days)
Hi,
I would like to use the mldivide "\" instead of inv in this formulae, but I am not sure how I should do it? I want to replace it for a matter of speed.
R = alpha * inv(B) * Q' * inv(I-((1-alpha)*P)) * Q;
The sizes of the matrices are the following:
n = total number of elements in a matrix
m = total number of unique elements in a matrix
P = n x n
I = n x n (identity matrix)
Q = n x m
B = n x m (diagonal matrix, sum of the rows in Q)
R = m x m
Any help would be greatly appreciated!
Thank you!
  2 Comments
Jean-Philippe
Jean-Philippe on 3 Jun 2012
Suggested Action (from Matlab help)
Instead of multiplying by the inverse, use matrix right division (/) or matrix left division (\). That is:
Replace inv(A)*b with A\b
Replace b*inv(A) with b/A
Shrinivas Iyengar
Shrinivas Iyengar on 14 Mar 2022
is a typo, right? It would be a $m \times m$ matrix.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jun 2012
If A is a square matrix, A\B is roughly the same as inv(A)*B, except it is computed in a different way.
Now your I-((1-alpha)*P) is certainly square, so you should be able to replace inv(I-((1-alpha)*P)) * Q by
(I-((1-alpha)*P)) \ Q
(but see below.)
When we scan further left to inv(B) we encounter a problem: you have B as n x m, not as square, so inv(B) does not exist, which leads to questions about the correctness of your original formula. You can have B on the left side of the "\" operator (the meaning is well defined) but you should reconsider your formula to see whether it makes sense. You might need to put () around portions, for example.
You need to be cautious because MATLAB processes operations of equal priority from left to right. Your formula
alpha * inv(B) * Q' * inv(I-((1-alpha)*P)) * Q
would be interpreted as
(((alpha * inv(B)) * Q') * inv(I-((1-alpha)*P))) * Q
Algebraically, with matrix multiplication, (A * B) * C is the same as A * (B * C), but numerically they are not the same. (alpha * (inv(B) * Q')) differs from ((alpha * inv(B)) * Q') proportional to eps(alpha^max(m,n)) [I think it is -- I could be wrong.] You thus need to consider the order you do the calculations in. For example, perhaps instead of using X * ((I-((1-alpha)*P)) \ Q) you should instead use (X / (I-((1-alpha)*P))) * Q
  4 Comments
Jean-Philippe
Jean-Philippe on 5 Jun 2012
Here is what I got:
R = inv(B) * Q' * ((speye(n)-(1-alpha)*P) \ (alpha*Q));
Walter Roberson
Walter Roberson on 5 Jun 2012
Is B still n x m ? If so then inv(B) does not exist. pinv(B) would exist but might not be what you want.
Perhaps you want B \ Q' instead of inv(B) * Q'

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!