Ask Your Question

Revision history [back]

For some reason - matrix multiplication crashes

I know this might have some trivial solution, and I might feel like an idiot, but I've spent hours on this and I just don't get what's wrong.

I am supposed to calculate a matrix, Q, according to an existing algorithm. I broke down the initial formulate to the most basic operations to debug end see where it fails - it fails on the 2nd step.

    Mat Ui = Mat::eye(4,4, CV_32F);
    Mat ui = Mat(4, 1, CV_32F);
    Mat P = Mat(2, 4, CV_32F);

    //Calculate Ui
    //Populate ui from an existing vector (sorry for the names.. it makes sense with context I swear)
    //Initialize P

    //Calculate Q according to the algorithm
    std::cout << "P: " << P.rows << "x" << P.cols << std::endl;
    std::cout << "Ui: " << Ui.rows << "x" << Ui.cols << std::endl;
    this->Q = Ui * P.t();
    std::cout << "here0 - Q: "<< this->Q.rows<<"x"<< this->Q.cols<< std::endl;
    this->Q = P * this->Q;
    std::cout << "here1 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
    this->Q = this->Q -(P * ui)*((P * ui).t());
    std::cout << "here2 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
    this->Q = this->Q.inv();
    std::cout << "here3 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
    this->Q = this->Q *(1.f / 4);

The debug shows:

P: 2x4
Ui: 4x4

Ui:
0.25 0 0 0
0 0.25 0 0
0 0 0.25 0
0 0 0 0.25
P:
23 45
85 22
72 91
24 22

ui: (0.25 0.25 0.25 0.25)

here0 - Q: 4x2

This is where it crushes. I have no idea what's wrong. It was earlier written as 1 line but I wrote it this way to try to debug.