Simulating matlab's mrdivide with 2 square matrices

后端 未结 2 1806
陌清茗
陌清茗 2021-01-06 15:29

I have 2 19x19 square matrices (a & b) and I am trying to use the slash (mrdivide) operator to perform a division such that

c = a / b

I

2条回答
  •  别那么骄傲
    2021-01-06 16:02

    In MATLAB, using mrdivide on two matrices of compatible dimension such that a / b is equivalent to a * b^{-1} where b^{-1} is the inverse of b. As such, what you could do is perhaps invert the matrix b first, then pre-multiply this a.

    One method is to use cv::invert on the matrix b then pre-multiplying with a. This could be done with the following function definition (borrowing from your code in your post above):

    cv::Mat mrdivide(const cv::Mat& A, const cv::Mat& B) 
    {
        cv::Mat bInvert;
        cv::invert(B, bInvert);
        return A * bInvert;
    }
    

    Another way is to use the inv() method that's builtin to the cv::Mat interface and just use that and multiply the matrices themselves:

    cv::Mat mrdivide(const cv::Mat& A, const cv::Mat& B) 
    {
        return A * B.inv();
    }
    

    I'm not sure which one is faster, so you may have to do some tests, but either of the two methods should work. However, to provide a bit of insight in terms of possible timings, there are three ways to invert a matrix in OpenCV. You simply override the third parameter to cv::invert or specify the method in cv::Mat.inv() to facilitate this.

    This StackOverflow post goes through the timings of inverting a matrix for a relatively large matrix size using the three methods: Fastest method in inverse of matrix

提交回复
热议问题