clc
I = imread('peppers.png'); I = double(I)/255;
[height,width,~] = size(I);
image = I;
noOfClasses = 5;
pDet = rand(noOfClasses,1);
pInv = rand(3,3,noOfClasses);
pMean = rand(noOfClasses,3);
disp('First do it the original way...')
tic
post(height,width,noOfClasses) = 0;
for count=1:height
for count2=1:width
x=[image(count,count2,1);image(count,count2,2);image(count,count2,3)];
for count3=1:noOfClasses
detZ = pDet(count3);
invZ = pInv(:,:,count3);
y=(pMean(count3,:)');
post(count,count2,count3)= 1/sqrt((2*pi)^2*detZ) * exp(-(x(:,1)-y)'*invZ*(x(:,1)-y)/2);
end
end
end
originalway = toc
disp('Now do it the fast way...')
RGB = reshape(I,[],3);
colorlist = RGB;
C = 1./sqrt((2*pi)^2*pDet);
ncol = size(colorlist,1);
tic
post2(height,width,noOfClasses) = 0;
for count3=1:noOfClasses
invZ = pInv(:,:,count3);
y=(pMean(count3,:)');
colorlistY = bsxfun(@minus,colorlist,y');
post2(:,:,count3) = reshape(C(count3) * exp(-sum(colorlistY*invZ.*colorlistY,2)/2) , height, width);
end
fasterway = toc
ARETHEYTHESAME = isequal(post,post2)
Best Answer