简体   繁体   English

矩阵乘法

[英]Matrix multiplication

I have a question which might be for beginners. 我有一个可能适合初学者的问题。

I want to multiply a 20x2 matrix by a 2x2 matrix in c++. 我想将20x2矩阵乘以c ++中的2x2矩阵。

I tried it with openCV but I get an error which is 我用openCV尝试过,但是出现错误

Bad argument (Unknown array type) in cvarrToMat cvarrToMat中的错误参数(未知数组类型)


Here is the code that I used in openCV in order to check the problem if it was with my code or the problem in the openCV but it still not working, I can compile with out error, but when I test the code I get the problem " Bad argument (Unknown array type) in cvarrToMat" 这是我在openCV中使用的代码,目的是检查问题是否出在我的代码中,还是在openCV中出现问题,但仍然无法正常工作,我可以编译时没有错误,但是当我测试代码时遇到了问题“ cvarrToMat中的错误参数(未知数组类型)”

#include <stdio.h>
#include <stdlib.h>
//#include "/usr/include/opencv/cv.h"
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
#include <math.h>
#include <iostream>

  int main()
{


double a[] = {1, 2, 3, 4};
CvMat Ma;
cvInitMatHeader(&Ma, 2, 2, CV_32FC1, a);


double b[] ={0, -1, 1, 0};

CvMat Mb;
cvInitMatHeader(&Mb, 2, 2, CV_32FC1, b);

CvMat Mc;
CvMat Mc1;
cvMatMul(&Ma, &Mb, &Mc);

return 0;
}

Comparing your code with the example in the OpenCV docs , it seems that you forgot to initialize the output matrix Mc : 将代码与OpenCV文档中示例进行比较,似乎您忘记了初始化输出矩阵Mc

double a[] = { 1, 2, 3, 4,
               5, 6, 7, 8,
               9, 10, 11, 12 };

double b[] = { 1, 5, 9,
               2, 6, 10,
               3, 7, 11,
               4, 8, 12 };

double c[9];
CvMat Ma, Mb, Mc ;

cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);
cvInitMatHeader(&Mb, 4, 3, CV_64FC1, b);
cvInitMatHeader(&Mc, 3, 3, CV_64FC1, c);

cvMatMulAdd(&Ma, &Mb, 0, &Mc);
// the c array now contains the product of a (3x4) and b (4x3)

According to the docs, cvMatMul(&Ma, &Mb, &Mc) is the same as cvMatMulAdd(&Ma, &Mb, 0, &Mc) . 根据文档, cvMatMul(&Ma, &Mb, &Mc)cvMatMulAdd(&Ma, &Mb, 0, &Mc)

Well. 好。 The answer to this question really depends on a few things. 这个问题的答案实际上取决于几件事。 You've said that you know how to do this by hand so to do this in code it will depend on how you represent your matrices. 您已经说过,您知道如何手动执行此操作,因此在代码中执行此操作将取决于您如何表示矩阵。 Now if this is a one time thing and you just need the answer to it, I would suggest a language like MATLAB that is built for this. 现在,如果这是一次性的事情,而您只需要解决的办法,我将建议为此而构建的语言,例如MATLAB。 If it is part of a larger program and you will be doing a lot of matrix multiplications that need to be efficient I recommend a high quality optimized library like boost::ublas . 如果它是一个较大程序的一部分,并且您将要进行很多需要高效的矩阵乘法,那么我建议您使用boost :: ublas这样的高质量优化库。

If it is a one time thing and you really want to do it in C++ and you really don't want/know how to use a third party library like ublas, a (not-optimized) matrix multiplication would like like the following: 如果这是一次性的事情,并且您真的想用C ++来做,并且您真的不想/不知道如何使用ublas这样的第三方库,那么(未优化的)矩阵乘法将如下所示:

template<typename T>
struct matrix2d
{
private:
    std::vector<std::vector<T>> data;
    size_t _rows, _columns;
public:
    matrix2d(size_t rows, size_t columns)
        :_rows(rows)
        ,_columns(columns)
    {
        data.resize(_rows, std::vector<T>(_columns));
    }

    size_t rows() const { return _rows; } 
    size_t columns() const { return _columns; } 

    T& operator()(size_t row, size_t column)
    {
        return data[row][column];
    }

    const T& operator()(size_t row, size_t column) const
    {
        return data[row][column];
    }
};

template<typename T>
void mmult(const matrix2d<T>& m1, const matrix2d<T>&m2, matrix2d<T>& result)
{
    for (size_t r = 0 ; r<m1.rows() ; ++r)
        for (size_t c = 0; c<m2.columns() ; ++c)
            for (size_t n = 0; n<m1.columns() ; ++n)
                result(r, c) = m1(r, n) * m2(n, c);
}


int main()
{

    matrix2d<double> m1(20, 2);
    matrix2d<double> m2(2, 2);
    matrix2d<double> result(m1.rows(), m2.columns());
    mmult(m1, m2, result);
}

May be you should post the prototype of the function you are calling, as well as the declaration of your matrices and your call. 可能是您应该发布要调用的函数的原型,以及矩阵的声明和调用。 I don't think everyone is familiar with a openCV. 我认为不是每个人都熟悉openCV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM