Use colons instead of loops. For instance:
for i3 = 1:2 for j3 = 1:2 MArr(windowSize*(j2-(j - floor(windowSize/2))+1) + (i2-(i - floor(windowSize/2)) + 1),i3,j3) = mat(i3,j3); end end
It can be written as:
MArr(windowSize*(j2-(j-floor(windowSize/2))+1)+(i2-(i-floor(windowSize/2))+1),:,:)=mat;
Once you find all the places where this can be done, learn to use indexing instead of a loop, for example,
i2 = i - floor(windowSize/2): i + floor(windowSize/2); i2=i2(i2>0 && i2<ysize+1); j2 = j - floor(windowSize/2): j + floor(windowSize/2); j2=j2(j2>0 && j2<xsize+1); mat = weight*[mappedGX(i2,j2)^2, mappedGX(i2,j2)*mappedGY(i2,j2);
(Note for advanced users: the last row may not work if mappedGX is a matrix, and i2 / j2 do not represent a rectangular submatrix. In this case, you need sub2ind() )
source share