clear all; close all; clc;
%% mean shift
% simplest mean shift example
% find centroid of 300 points
%test data
mu=[0 0]; %average
S=[30 0;0 35]; %coff
data=mvnrnd(mu,S,300); %generate test data cov(data) similar to S
plot(data(:,1),data(:,2),'o');
% para set
h=3; %kernel
x=[data(1,1) data(1,2)]; %iter init value
pre_x=[0 0];
% iteration
hold on
res = [];
while norm(pre_x-x)>0.0001
res = [res norm(pre_x-x)];
pre_x=x;
plot(x(1),x(2),'r+');
u=0; % nuerator sum
d=0; %denominator sum
for i=1:300
%mean shift realization
k=norm((x-data(i,:))/h).^2;
g=(1/sqrt(2*pi))*exp(-0.5*k);
u=data(i,:)*g+u;
d=g+d;
end
M=u/d; %position after iteration
x=M;
end
hold off
figure
plot(res)