Can anyone help me to reduce number of for loops in this code ?

2 views (last 30 days)
for n = 1:KMI
for i = 1:M
for j = 1:N
for k=1:noC
dis(k,1)= sqrt(sum((reshape(im(i,j,:),[1,noF])-centers(k,:)) .^2,2));
end
[minDis, index] = min(dis(1:noC,1));
im2d(i,j)=index;
im_mind(i,j)=minDis;
bestfit = sum(im_mind(:));
end
end
plus=0;
noValues=0;
mean=zeros(noC,noF);
for z=1:noF
for k=1:noC
noValues=0;
plus=0;
for i = 1:M
for j = 1:N
if(im2d(i,j)==k)
plus=plus+im(i,j,z);
noValues=noValues+1;
end
end
end
if(noValues==0)
mean(k,z)=0;
else
mean(k,z)=(plus/noValues);
end
end
end
if(centers==mean)
break;
end
centers=mean;
end
  3 Comments
Akhil Movva
Akhil Movva on 12 Nov 2019
im is an array of pixel values,
centers is an array of geometric coordinates,
actually i don't know this rgb2ind() funtion.
Walter Roberson
Walter Roberson on 12 Nov 2019
It does not make sense to compare color information im(i,j,:) to geometric locations.
It would make sense to compare color information im(i,j,:) to color information for a given list of colors, in order to determine the "closest" color. It happens that that is what rgb2ind() can do for you.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 12 Nov 2019
for n = 1:KMI
for i = 1:M
for j = 1:N
for k=1:noC
dis(k,1)= sqrt(sum((reshape(im(i,j,:),[1,noF])-centers(k,:)) .^2,2));
end
[minDis, index] = min(dis(1:noC,1));
im2d(i,j)=index;
im_mind(i,j)=minDis;
bestfit = sum(im_mind(:));
end
end
You overwrite bestfit in every iteration of j inside every iteration of i inside every iteration of n . If you look at the rest of your code, you will find that you never use bestfit afterwards. This means that you only want the final value to use afterwards for some reason -- the value associated with n = KMI, i = M, j = N. So you can save resources by not doing the calculations for bestfit inside the loops, and instead do it afterwards with n=KMI, i=M, j=N only.
You are overwriting im_mind(i,j) inside for n for i for j, and you do not use im_mind inside the for n loop, except to calculate bestfit, which as discussed above, you do not use inside any of the loops. You can save resources by not doing the calculation inside the for n loop. Instead, just calculate im_mind once, with n=KMI, and the entire range of i and j values, creating only one single im_mind array. Then after that has been done, you can calculate bestfit once as discussed above. More likely you do not need im_mind or bestfit at all.
You do do further computation with the im2d index array. As I described in the comments, you can build it by a single call to rgb2ind()
I do not immediately follow the code for the loops after that point. It looks like for each color, you are calculating the mean R, G, or B component from the image from the pixels that were considered to match that color. If that is correct, then there much more efficient ways to do that:
for K = NoC : -1 : 1
output_mean(:,K) = accumarray(im2d(:), reshape(im(:,:,K), [], 1), [NoC,1], @mean, 0);
end
Then you have
if(centers==mean)
break;
end
centers=mean;
Is it possible that all of this code is an attempt to find the noC "best" colors to partition the image into? If so, then you can replace the entire code with a call to rgb2ind(im, noC)
  2 Comments
Akhil Movva
Akhil Movva on 12 Nov 2019
Thankyou for you answering. It was very helpful. The above code is part of k-means clustering program for 3 feature and 5 feature image
Walter Roberson
Walter Roberson on 12 Nov 2019
Just call kmeans() directly instead of programming it yourself.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!