Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For two matrices, A and B, with dimensions {n,p} and {n,2} respectively, LeastSquares[A,B] returns an matrix, R, with dimensions {p,2}.

But any row of R could be anywhere in the real plane. I want to constrain R[[1]] and R[[-1]] to two different lines. How should this be done?

The system is overdetermined, n>p

share|improve this question
Seems like "random" cases will have them linearly independent. Trying to force this could be tricky, unless you have a criterion in mind for how far apart they must be. Determining what that might mean, for your purposes, would be the first step. – Daniel Lichtblau Jan 31 '14 at 16:23

Here is an example of one way to proceed.

n = 3; p = 4;
amat = Array[a, {n, p}];
bvec1 = Array[b1, n];
bvec2 = Array[b2, n];
rvec1 = Array[r1, p];
rvec2 = Array[r2, p];
sol = Solve[amat.Transpose[{rvec1, rvec2}] == Transpose[{bvec1, bvec2}], 
  Flatten[{rvec1, rvec2}]]

The solution you get from this comes with a warning that "Equations may not give solutions for all "solve" variables." This is precisely what you want to exploit. For the given n and p, you have free variables r1[1] and r2[1], which can be set to any desired value, thus giving the constraint that you specify. As you change n and p, you may have a larger or smaller number of free variables.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.