LSQLIN gives error even when A\b works

7 次查看(过去 30 天)
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

采纳的回答

John D'Errico
John D'Errico 2015-4-1
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Least Squares 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by