MATLAB Answers


Solving Systems of Linear Equations

Asked by GEO GEP
on 25 Apr 2012


I m trying to solve XA=B where both A,B are matrix (instead of B being a vector) Using e.g. LU decomposition ('linsolve' or '/') is possible to obtain such a solution.

However i need to constrain X>0.

Is this an optimization problem (min(||XA-B||),X>0,B), and if it is can someone propose a suitable function ?

Thank you



No products are associated with this question.

3 Answers

Answer by Teja Muppirala
on 26 Apr 2012
 Accepted answer

Solving for each row of X is an independent optimization problem that can be solved easily with LSQNONNEG (available from the Optimization Toolbox). Use a loop to solve for each row independently.

Example 1 (test when know the exact answer):

% Set up some data
A = rand(5);
Xtrue = rand(5); 
B = Xtrue*A;
% Solve for each row of X using LSQNONNEG
X = [];
for k = 1:size(B,1)
    X(k,:) = lsqnonneg(A',B(k,:)');
% Verify the result
X - Xtrue

Example 2:

A = rand(6,3);
B = rand(6,3);
X = [];
for k = 1:size(B,1)
    X(k,:) = lsqnonneg(A',B(k,:)');
% Verify that all X are positive

Note that if your data is very big, this algorithm could easily be sped up by running it in parallel.


(I was lazy, but in general you'd probably want to preallocate X here since you do know its size)

Good point, Richard

on 26 Apr 2012

If A=3x3, B=3x3 (and X=3x3), then as Richard said X = BA^{-1}, and either will or will not violate the constraints (there's nothing I can do about it).

However my system can have an arbitrary number of columns where A=3x(3*n), B=3x(3*n), n E R (and X=3x3). If i understand correctly both problems can be tackled with multiple lsqnonneg (or linprog)...

Can this problem be (also) solved by a non negative matrix factorization nnmf (B=W*H, enforcing somehow H=A)

Thank you -so much- for your answers

Answer by bym
on 25 Apr 2012


Answer by Richard Brown
on 25 Apr 2012

It very much depends on your matrices. What are the dimensions? Rank?

If A square and full rank then X is uniquely determined as X = BA^{-1}, and either will or will not violate the constraints (there's nothing you can do about it).


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!