I have a for loop nested thrice in a matlab program. Can any of you help me optimize it.
w=5;
a = rand(m*n,10); b=rand(m,n);
for i = 1 : m
for j = 1 : n
for k = 1 : l
if (i-w >= 1 && i+w <= m)
featureL = a(((i-1)*n)+j,:); featureR = a(((i-1)*n)+j-d,:);
D1(i,j,k) = sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) );
D2(i,j,k) = mean2( b(i-w:i+w, j-w:j+w) );
end
end
end
end
I know the performance could be heavily improved by using meshgrid, but I am not sure how to do it.
Thanks in anticipation.
Can it be done something like this..
[X Y Z] = meshgrid(1:m,1:n,1:l);
D1(something containing X,Y,Z) = sqrt( sum( ( a(something cont. X,Y) - a(something cont. X,Y)).*(a(something cont. X,Y) - a(something cont. X,Y)) ) );
% similarly D2
Thanks a lot!.
for i=1+w:m-w.m,n,landdthat you're working with?kon the calculations. As far as I can see all pages are the same -> userepmat.mlint your_file_nameIt gives lots of warnings you should preallocate D1 and D2.d? is it the same asw? also how are the formulas depending onk? Please double check your code..