Matrix multiplication using 1d arrays

南楼画角 提交于 2019-12-04 22:53:05

问题


I'm trying to multiply two matrices stored inside 1d arrays.

I'm using this function, but my program crashes, I assume due to an out of bounds error. However, I have no (easy) ability to debug, so I have to decide if my code is correct, and to me it seems it is...

void SampleUtils::multiplyMatrices(float* matA, int rA, int cA, float* matB,
        int rB, int cB, float* matC, int rC, int cC) {
    for (int i = 0; i <= rA; i++) {
        for (int j = 0; j <= cB; j++) {
            float sum = 0.0;
            for (int k = 0; k <= rB; k++)
                sum = sum + matA[i * cA + k] * matB[k * cB + j];
            matC[i * cC + j] = sum;
        }

    }

So, can anyone find out what I did wrong?

Thanks...


回答1:


Chances are you mean < instead of <= in your for loops.




回答2:


Try to use i < rA , j < cB, k < rB in your for



来源:https://stackoverflow.com/questions/10252621/matrix-multiplication-using-1d-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!