How to compute a fast outer product between two matrices, in Matlab?

后端 未结 3 851
情歌与酒
情歌与酒 2021-01-07 02:27

I have two n-by-m matrices, A and B. I want to create a new matrix C which is something like:

for i = 1:n
    C = C +          


        
3条回答
  •  温柔的废话
    2021-01-07 03:26

    Have you profiled your for loop code and found it to be too slow? If not, do it before you spend too much time agonizing over the loop penalty.

    Your for loop is not particularly bad because you loop only n times but do O(n*m) work each loop. Since you're doing a lot of work each iteration, the loop penalty doesn't hit as hard. The really bad situations are nested loops, e.g. if you calculated the outer products with nested for loops too.

提交回复
热议问题