Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

2D matrix multiplication in 2D matrix error

 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?

2D matrix multiplication in 2D matrix error

 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? image description

2D matrix multiplication in 2D matrix error

 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? image description

i'll update my code which is now creating the matrices i want the multiplication is different from matlab's result matlab's version :

X =

1.0e+03 *

0.0010    1.0743
0.0010    1.0740

X'

ans =

1.0e+03 *

0.0010    0.0010
1.0743    1.0740

X'*X

ans =

1.0e+06 *

0.0000    0.0021
0.0021    2.3075

my new openCV version

CentroidXY Utilities::LinearMapping(std::vector<double> x0, std::vector<double> y0)
{


    cv::Mat X = cv::Mat::ones(x0.size(),2, CV_32F);
    for (int i = 0; i < x0.size(); i++)
    {
        X.at<float>(i,1) = x0[i];
    }       
    cv::Mat XTrans;

    cv::transpose(X,XTrans);
    cv::Mat invMat;
    /*% Solving b = (X'*X)^-1*X'*y = inv(X'*X)*X'*y*/

    cv::Mat b = XTrans *X;

}

here is my output:

 X: 
[1, 1074.272;
  1, 1073.9705]

 X': 
[1, 1;
  1074.272, 1073.9705]

 X'*X 
[2, 2148.2424;
  2148.2424, 2307472.8]

once again what is the difference? I have also tried XTrans.mul(X)

2D matrix multiplication in 2D matrix error

 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? image description

i'll update my code which is now creating the matrices i want the multiplication is different from matlab's result matlab's version :

X =

1.0e+03 *

0.0010    1.0743
0.0010    1.0740

X'

ans =

1.0e+03 *

0.0010    0.0010
1.0743    1.0740

X'*X

ans =

1.0e+06 *

0.0000    0.0021
0.0021    2.3075

inv(X'*X)

25402233.6743484    -23649.3170474466
-23649.3170474466   22.0173633149477

my new openCV version

CentroidXY Utilities::LinearMapping(std::vector<double> x0, std::vector<double> y0)
{

{       
    cv::Mat X = cv::Mat::ones(x0.size(),2, CV_32F);
    for (int i = 0; i < x0.size(); i++)
    {
        X.at<float>(i,1) = x0[i];
    }       
    cv::Mat XTrans;

    cv::transpose(X,XTrans);
    cv::Mat invMat;
    /*% Solving b = (X'*X)^-1*X'*y = inv(X'*X)*X'*y*/

    cv::Mat b = XTrans *X;
*X; 
    invMat  =b.inv();   
}

here is my output:

 X: 
[1, 1074.272;
  1, 1073.9705]

 X': 
[1, 1;
  1074.272, 1073.9705]

 X'*X 
[2, 2148.2424;
  2148.2424, 2307472.8]

 inv(X'*X)
[-51162084, 47631.574;
  47631.574, -44.344692]

once again what is the difference? I have also tried XTrans.mul(X)

2D matrix multiplication in 2D matrix error

 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? image description

i'll update my code which is now creating the matrices i want the multiplication is different from matlab's result matlab's version :

X =

1.0e+03 *

0.0010    1.0743
0.0010    1.0740

X'

ans =

1.0e+03 *

0.0010    0.0010
1.0743    1.0740

X'*X

ans =

1.0e+06 *

0.0000    0.0021
0.0021    2.3075

inv(X'*X)

25402233.6743484    -23649.3170474466
-23649.3170474466   22.0173633149477

my new openCV version

CentroidXY Utilities::LinearMapping(std::vector<double> x0, std::vector<double> y0)
{       
    cv::Mat X = cv::Mat::ones(x0.size(),2, CV_32F);
    for (int i = 0; i < x0.size(); i++)
    {
        X.at<float>(i,1) = x0[i];
    }       
    cv::Mat XTrans;

    cv::transpose(X,XTrans);
    cv::Mat invMat;
    /*% Solving b = (X'*X)^-1*X'*y = inv(X'*X)*X'*y*/

    cv::Mat b = XTrans *X; 
    invMat  =b.inv();   
}

here is my output:

 X: 
[1, 1074.272;
  1, 1073.9705]

 X': 
[1, 1;
  1074.272, 1073.9705]

 X'*X 
[2, 2148.2424;
  2148.2424, 2307472.8]

 inv(X'*X)
[-51162084, 47631.574;
  47631.574, -44.344692]

once again what is the difference?

2D matrix multiplication in 2D matrix error

UPDATE I have a found a solution to my problem I need to work with CV_64F / double and everything is working fine.

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'll update my code which is now creating the matrices i want the multiplication is different from matlab's result matlab's version :

X =

1.0e+03 *

0.0010    1.0743
0.0010    1.0740

X'

ans =

1.0e+03 *

0.0010    0.0010
1.0743    1.0740

X'*X

ans =

1.0e+06 *

0.0000    0.0021
0.0021    2.3075

inv(X'*X)

25402233.6743484    -23649.3170474466
-23649.3170474466   22.0173633149477

my new openCV version

CentroidXY Utilities::LinearMapping(std::vector<double> x0, std::vector<double> y0)
{       
    cv::Mat X = cv::Mat::ones(x0.size(),2, CV_32F);
    for (int i = 0; i < x0.size(); i++)
    {
        X.at<float>(i,1) = x0[i];
    }       
    cv::Mat XTrans;

    cv::transpose(X,XTrans);
    cv::Mat invMat;
    /*% Solving b = (X'*X)^-1*X'*y = inv(X'*X)*X'*y*/

    cv::Mat b = XTrans *X; 
    invMat  =b.inv();   
}

here is my output:

 X: 
[1, 1074.272;
  1, 1073.9705]

 X': 
[1, 1;
  1074.272, 1073.9705]

 X'*X 
[2, 2148.2424;
  2148.2424, 2307472.8]

 inv(X'*X)
[-51162084, 47631.574;
  47631.574, -44.344692]

once again what is the difference? difference?