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
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