Utilities::LinearMapping(std::vector<double> x0, std::vector<double> y0)
{
cv::Mat xInput = cv::Mat::zeros(x0.size(),1, CV_32F);
cv::Mat xOnes = cv::Mat::ones(x0.size(),1, CV_32F);
for (int i = 0; i < x0.size(); i++)
{
xInput.at<float>(i) = x0[i];
}
cv::Mat input[] = {xOnes,xInput};
cv::Mat X;
cv::Mat XTrans;
cv::merge(input,2,X);
cv::transpose(X,XTrans);
cv::Mat invMat;
/*% Solving b = (X'*X)^-1*X'*y = inv(X'*X)*X'*y*/
cv::Mat b = XTrans.mul(X); //crash is here
.....
}
My input are two double vectors
I want to create b = (X'*X)^-1*X'*y = inv(X'*X)*X'*y*
the original matlab code is :
function c = linear_mapping(x0, y0)
% Reshape input values just to make sure they are column order
x0 = x0(:); % column wise order
y0 = y0(:); % column wise order
% Linear mapping y = X*b, where X = [1, x] and b are coefficients
% Solving b = (X'*X)^-1*X'*y = inv(X'*X)*X'*y
X = [ones(size(x0)), x0];
b = linsolve(X, y0);
c(1) = b(2);
c(2) = b(1);
end
I want to build vector of ones and combine it with x0 into a nx2 matrix
then I want to do X'*X
however I'm crashing;
X = float32 2x1X2
XTrans = float32 2x2X1
what am I doing wrong?