LSQLIN gives error even when A\b works

3 views (last 30 days)
Hello everyone
i have a set of linear equations which i would like to solve. My data consist of two matrices, Components and fitData.
>> size(Components)
ans =
812 2
>> size(fitData)
ans =
812 9
now if i enter coef= Components\fitData i get a 2x9 matrix with the coefficient for the two components for all 9 data vectors in fitData. in other words, Components*coef gives me a good fit the the data in fitData
But i would like to restrict the coefficients to the interval [1,0] and ensure that each pair of coefficients sum to 1 - think of it like 40% of first component and 60% of second component. for this, i have tried to use LSQLIN but even the simplest call give me error
>> coef = lsqlin(Components,fitData);
Error using \
Matrix dimensions must agree.
Error in lsqlin (line 292)
X = C\d;
but i just did Components\fitData above and i works fine. Does anyone know why LSQLIN does not work when normal "\"do? any help would be greatly appreciated :)
if two questions are allowed in one thread, i would also like some input to how i can formulate the constraint that require the sum of coefficients to be 1
i use MATLAB R2014a and have optimization toolbox installed.
EDIT: it appears that LSQLIN require that d is a nx1 vector. if i use the following code it works (the coef. are overwritten in this example)
for k = 1:size(fitData,2)
coef = lsqlin(Components,fitData(:,k));
end
so now i can get that to work at least - now i just need to figure out how to require the pair of coef. to sum to 1

Accepted Answer

John D'Errico
John D'Errico on 1 Apr 2015
The problem is in your right hand side (fitData).
LSQLIN does NOT solve problems with multiple right hand sides. Essentially, it requires the d argument to be a VECTOR, NOT an array with 9 columns.
size(fitData)
ans =
812 9
You have 9 separate systems to solve, 9 distinct problems. Put a loop around it.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!