Multiplying two matrices with different dimensions

牧云@^-^@ 提交于 2019-12-13 07:16:03

问题


I am writing an application to multiply matrices. This works nicely as intended for matrices a and b that are nxn:

for(k = 0; k < n; k++) {
    for(i = 0; i < n; i++) {
        tmp = a[i][k];
        for(j = 0; j < n; j++) {
            c[i][j] = c[i][j] + tmp * b[k][j];
        }
    }
}

If a was nxy and b was yxm (implying c to be nxm). How would I modify the above loop to work?

Thanks


回答1:


This should work:

for(k = 0; k < y; k++) {
    for(i = 0; i < n; i++) {
        tmp = a[i][k];
        for(j = 0; j < m; j++) {
            c[i][j] = c[i][j] + tmp * b[k][j];
        }
    }
}


来源:https://stackoverflow.com/questions/10163948/multiplying-two-matrices-with-different-dimensions

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