Vectorizing nested loops in matlab using bsxfun and with GPU
问题 For loops seem to be extremely slow, so I was wondering if the nested loops in the code shown next could be vectorized using bsxfun and maybe GPU could be introduced too. Code %// Paramaters i = 1; j = 3; n1 = 1500; n2 = 1500; %// Pre-allocate for output LInc(n1+n2,n1+n2)=0; %// Nested Loops - I for x = 1:n1 for y = 1:n1 num = ((n2 ^ 2) * (L1(i, i) + L2(j, j) + 1)) - (n2 * n * (L1(x,i) + L1(y,i))); LInc(x, y) = L1(x, y) + (num/denom); LInc(y, x) = LInc(x, y); end end %// Nested Loops - II for