How to find minimum distance from one set of points to another set of points in matlab? - matlab

I have distributed 100 random points(nodes) in an area by using following code
n=100; xm=100;ym=100;
for i = 1 : 1 :n
S(i).xd = rand(1,1)*xm ;
S(i).yd = rand (1,1)*ym ;
xd(i) = S(i).xd ;
yd(i) = S(i).yd ;
S(i).id = i;
S(i).type = 0;
S(i).g = 0 ;
S(i).E = Eo ;
S(i).type = 0 ;
end
Next I have fixed 10 gateway nodes at the edge of the area using following code
for i=1:1:10
Sg(i).xd= 2+100*rand(1,1);
Sg(i).yd=100;
xd(i)=Sg(i).xd;
yd(i)=Sg(i).yd;
Sg(i).id=i;
plot(Sg(i).xd,Sg(i).yd,'*k')
grid on;
hold on;
end
Now I have formed cluster heads using LEACH protocol from 100 nodes.
I have to find min distance gateway node from a CH. As there are 10 gateway nodes, I have to find which is closer to a CH in the area.
To determine CH I have used following code
for all nodes in the area
temp_rand1=rand;
if(temp_rand1<= (p/(1-p*mod(r,round(1/p)))))
countCHs1=countCHs1+1;
S3(i).type=1;
S3(i).g=round(1/p)-1;
C1(cluster1).xd=S3(i).xd;
C1(cluster1).yd=S3(i).yd;
**distance1=sqrt((S3(i).xd-(gw_node.x) )^2 + (S3(i).yd-(gw_node.y) )^2 );**
C1(cluster1).distance1=distance1;
C1(cluster1).id=i;
X(cluster1)=S3(i).xd;
Y(cluster1)=S3(i).yd;
cluster1=cluster1+1;
end
I know how to determine distance between a CH and one gateway node, But I do not know how to find closet gateway node from a set of gateway nodes.
Thanks in advance
Please reply

Not sure what you are asking. I assume you have set of fixed points in a straight line (marked blue) and set of random points (marked red). You want the Minimal distance between one fixed and one random point.
Try this code:
si = randi([0,100],10,2); %// Random points (red)
sg(:,1) = randi([0,100],10,1); %// fixed points x param (blue)
sg(:,2) = ones(10,1)*50; %// fixed points y param (blue) r const, to make them a st. line
sgc = mat2cell(sg,ones(1,size(sg,1)),size(sg,2)); %// converting to cell array
sic = mat2cell(si,ones(1,size(si,1)),size(si,2)); %// converting to cell array
%// distMat gives n*n matrix of all the dist between each fixed point with every other point.
distMat = cellfun(#(x,y) pdist2(x,y),repmat(sic,1,size(sgc,1)),repmat(sgc',size(sic,1),1));
figure; axis('image');
plot(si(:,1),si(:,2),'r.');
text(si(:,1),si(:,2),[repmat(' ',size(si,1),1), num2str((1:size(si,1))')]);
hold on
plot(sg(:,1),sg(:,2),'b.');
text(sg(:,1),sg(:,2),[repmat(' ',size(sg,1),1), num2str((1:size(sg,1))')]);
hold off
[mdist, index] = min(distMat(:));
[randomP, fixedP] = ind2sub(size(distMat),index)
Plot:
output:
randomP =
2
fixedP =
2
>> mdist
mdist =
6.7082

Not too sure what you are asking for but I think you want to find the two closest points.
This is how you could do it.
%// Generate some points
n = 3; xm = 1:n; ym =1:n;
m = [xm' ym']; %// This is the line
xd = [ 2 8 3];
yd = [ 1 4 3];
d = [xd' yd']; %// These are the random points
drep = repmat(d,n,1); %// This will repeat the matrix d n times
B= ones(n,1);
mrep = kron(m,B); %// This will repeat each value in m n times
distances = bsxfun(#minus,mrep,drep); %// The distance from each point to every other point
C_squared = sum(distances.^2,2)
[min_dist,idx] = min(sum(distances.^2,2)) %// Find the minimum distance
value_in_m = mrep(idx,:)
ans =
3 3
value_in_d = drep(idx,:)
ans =
3 3
Explanation
% Generate some points
n = 3;
xm = 1:n;
ym =1:n;
m = [xm' ym']; %// This is the line. Each row is a point
xd = [ 2 8 3];
yd = [ 1 4 3];
d = [xd' yd']; %// These are the random points
We want to calculate the distance from one point to every
other point so we repeat the points nxn times
Below is just one way of getting repeated elements.
More suggestions are here: https://stackoverflow.com/a/16269185
drep = repmat(d,n,1); %// This will repeat the matrix d n times
B= ones(n,1);
mrep = kron(m,B); %// This will repeat each value in m n times
We are basing this on Pythagoras theorem so (m_x-d_x)^2 + (m_y-d_y)^2 = C^2
distances = bsxfun(#minus,mrep,drep); %// The distance from each point to every other point
C_squared = sum(distances.^2,2)
Find the minimum distance
[min_dist,idx] = min(sum(distances.^2,2));
Find the points with the minimum distance
value_in_m = mrep(idx,:)
ans =
3 3
value_in_d = drep(idx,:)
ans =
3 3

Related

PDF and CDF plot for central limit theorem using Matlab

I am struggling to plot the PDF and CDF graphs of where
Sn=X1+X2+X3+....+Xn
using central limit theorem where n = 1; 2; 3; 4; 5; 10; 20; 40
I am taking Xi to be a uniform continuous random variable for values between (0,3).
Here is what i have done so far -
close all
%different sizes of input X
%N=[1 5 10 50];
N = [1 2 3 4 5 10 20 40];
%interval (1,6) for random variables
a=0;
b=3;
%to store sum of differnet sizes of input
for i=1:length(N)
%generates uniform random numbers in the interval
X = a + (b-a).*rand(N(i),1);
S=zeros(1,length(X));
S=cumsum(X);
cd=cdf('Uniform',S,0,3);
plot(cd);
hold on;
end
legend('n=1','n=2','n=3','n=4','n=5','n=10','n=20','n=40');
title('CDF PLOT')
figure;
for i=1:length(N)
%generates uniform random numbers in the interval
X = a + (b-a).*rand(N(i),1);
S=zeros(1,length(X));
S=cumsum(X);
cd=pdf('Uniform',S,0,3);
plot(cd);
hold on;
end
legend('n=1','n=2','n=3','n=4','n=5','n=10','n=20','n=40');
title('PDF PLOT')
My output is nowhere near what I am expecting any help is much appreciated.
This can be done with vectorization using rand() and cumsum().
For example, the code below generates 40 replications of 10000 samples of a Uniform(0,3) distribution and stores in X. To meet the Central Limit Theorem (CLT) assumptions, they are independent and identically distributed (i.i.d.). Then cumsum() transforms this into 10000 copies of the Sn = X1 + X2 + ... where the first row is n = 10000copies of Sn = X1, the 5th row is n copies of S_5 = X1 + X2 + X3 + X4 + X5. The last row is n copies of S_40.
% MATLAB R2019a
% Setup
N = [1:5 10 20 40]; % values of n we are interested in
LB = 0; % lowerbound for X ~ Uniform(LB,UB)
UB = 3; % upperbound for X ~ Uniform(LB,UB)
n = 10000; % Number of copies (samples) for each random variable
% Generate random variates
X = LB + (UB - LB)*rand(max(N),n); % X ~ Uniform(LB,UB) (i.i.d.)
Sn = cumsum(X);
You can see from the image that the n = 2 case, the sum is indeed a Triangular(0,3,6) distribution. For the n = 40 case, the sum is approximately Normally distributed (Gaussian) with mean 60 (40*mean(X) = 40*1.5 = 60). This shows the convergence in distribution for both the probability density function (PDF) and the cumulative distribution function (CDF).
Note: The CLT is often stated with convergence in distribution to a Normal distribution with zero mean as it has been shifted. Shifting the results by subtracting mean(Sn) = n*mean(X) = n*0.5*(LB+UB) from Sn gets this done.
Code below isn't the gold standard but it produced the image.
figure
s(11) = subplot(6,2,1) % n = 1
histogram(Sn(1,:),'Normalization','pdf')
title(s(11),'n = 1')
s(12) = subplot(6,2,2)
cdfplot(Sn(1,:))
title(s(12),'n = 1')
s(21) = subplot(6,2,3) % n = 2
histogram(Sn(2,:),'Normalization','pdf')
title(s(21),'n = 2')
s(22) = subplot(6,2,4)
cdfplot(Sn(2,:))
title(s(22),'n = 2')
s(31) = subplot(6,2,5) % n = 5
histogram(Sn(5,:),'Normalization','pdf')
title(s(31),'n = 5')
s(32) = subplot(6,2,6)
cdfplot(Sn(5,:))
title(s(32),'n = 5')
s(41) = subplot(6,2,7) % n = 10
histogram(Sn(10,:),'Normalization','pdf')
title(s(41),'n = 10')
s(42) = subplot(6,2,8)
cdfplot(Sn(10,:))
title(s(42),'n = 10')
s(51) = subplot(6,2,9) % n = 20
histogram(Sn(20,:),'Normalization','pdf')
title(s(51),'n = 20')
s(52) = subplot(6,2,10)
cdfplot(Sn(20,:))
title(s(52),'n = 20')
s(61) = subplot(6,2,11) % n = 40
histogram(Sn(40,:),'Normalization','pdf')
title(s(61),'n = 40')
s(62) = subplot(6,2,12)
cdfplot(Sn(40,:))
title(s(62),'n = 40')
sgtitle({'PDF (left) and CDF (right) for Sn with n \in \{1, 2, 5, 10, 20, 40\}';'note different axis scales'})
for tgt = [11:10:61 12:10:62]
xlabel(s(tgt),'Sn')
if rem(tgt,2) == 1
ylabel(s(tgt),'pdf')
else % rem(tgt,2) == 0
ylabel(s(tgt),'cdf')
end
end
Key functions used for plot: histogram() from base MATLAB and cdfplot() from the Statistics toolbox. Note this could be done manually without requiring the Statistics toolbox with a few lines to obtain the cdf and then just calling plot().
There was some concern in comments over the variance of Sn.
Note the variance of Sn is given by (n/12)*(UB-LB)^2 (derivation below). Monte Carlo simulation shows our samples of Sn do have the correct variance; indeed, it converges to this as n gets larger. Simply call var(Sn(40,:)).
% with n = 10000
var(Sn(40,:)) % var(S_40) = 30 (will vary slightly depending on random seed)
(40/12)*((UB-LB)^2) % 29.9505
You can see the convergence is very good by S_40:
step = 0.01;
Domain = 40:step:80;
mu = 40*(LB+UB)/2;
sigma = sqrt((40/12)*((UB-LB)^2));
figure, hold on
histogram(Sn(40,:),'Normalization','pdf')
plot(Domain,normpdf(Domain,mu,sigma),'r-','LineWidth',1.4)
ylabel('pdf')
xlabel('S_n')
Derivation of mean and variance for Sn:
For the expectation (mean), the second equality holds by linearity of expectation. The third equality holds since X_i are identically distributed.
The discrete version of this is posted here.

mean of multiple rows (points) in matlab

I have a matrix named data with x,y,z positions.
I have calculated the distance between all the points using pdist and got the matrix 'out' which has the diff of x, diff of y and diff of z and their corresponding rows.
A= data;
D1= pdist(A(:,1));
D2= pdist(A(:,2));
D3= pdist(A(:,3));
D = [D1' D2' D3'];
tmp = ones(size(A,1));
tmp = tril(tmp,-1);
[rowIdx,colIdx ] = find(tmp);
out = [D,A(rowIdx,:),A(colIdx,:)];
I want to calculate the average of all those points which satisfies certain conditions:
diff z <7 and diff x and diff y < 4.
So I wrote the following code:
a= find (out(:,3)>0);
cal=out(a,:);
b= cal(:,3)<7;
cal2 = cal(b,:);
[s,k]= size (cal2);
for i=1:s
if (cal2(i,1) < 4) && (cal2(i,2) < 4);
xmean = mean (cal2(i,[4,7]));
ymean = mean (cal2(i,[5,8]));
zmean = mean (cal2(i,[6,9]));
fd = [xmean ymean zmean];
end
end
The problem is, with this code I can get the mean of two points at a time. So, my output is giving me more number of points than what I actually want.I want to get the mean off all the points which satisfies the condition.
My final goal is to get a list of points where I can get the points whose (diff z is > 7 )+ (the mean of the points whose diff z is <7 while diff x < 4 and diff y <4) + (diff z < 7 but diff x and diff y > 4).
Can someone please help?
A = [0,0,0;0,0,5;0,0,10];
% create matrices with the distances
% [p1-p1, p1-p2, p1-p3;
% p2-p1, p2-p2, p2-p3;
% p3-p1, p3-p2, p3-p3];
dx= squareform(pdist(A(:,1)));
dy= squareform(pdist(A(:,2)));
dz= squareform(pdist(A(:,3)));
% dz = [0,5,10;5,0,5;10,5,0];
% select all points stisfying condition
idx = dx < 4 & dy < 4 & dz < 7;
% remove entries of the main diagonal
idx(sub2ind(size(idx),1:size(A,1),1:size(A,1))) = 0;
% select all indices. Since all distances are 2 times in the full matrix,
% only r or c is needed
[r,c] = find(idx==1);
% r = [2,1,3,2]; c = [1,2,2,3]
meanOfPoints = mean(A(r,:))
Say you want the mean of all points where diffZ<7, diffX<4 and diffY<4. Assuming that out stores the corresponding differences in x,y,z in the first three columns, and point coordinates in columns 4-9.
%# this is true for all points you want to average
condition = out(:,1) > 4 & out(:,2) > 4 & out(:,3) < 7;
%# get coordinates satisfying the condition; create a 2n-by-3 array
coords = [out(condition, 4:6);out(condition, 7:9)];
%# if you don't want to count coordinates multiple times when taking the average, run
%# coords = unique(coords,'rows');
%# average
meanCoordinates = mean(coords,1);

Replicate vector and shift each copy by 1 row down without for-loop

I would like replicate a vector N times to create a matrix with each copy shifted 1 row down. See image (first column is the vector 1 to 5). It would be great if this can be achieved without using for loop.
So far was able to to do this repmat(my_vector, 1, 5) to create an N x 5 matrix.
You can do it with toeplitz and tril;
a = [1 2 3 4 5]
out = tril( toeplitz(a) )
or
out = toeplitz(a, a*0)
%// out = toeplitz(a, zeros(size(a)) ) %// for large arrays
or if you don't mind some happy flipping:
out = flipud( hankel( flipud(a(:)) ) )
Solution Code
This seems to be a fast approach based on repmat and bsxfun as the benchmarks listed in the next section might convince us -
%// Concatenate one zero at the end of a column vector version of the input vector.
%// Then, replicate the whole vector along columns to have a 2D matrix.
%// Then "progressively" set elements from each column as zeros corresponding
%// to the starting zeros of the desired output.
val = repmat([A(:);0],1,N).*bsxfun(#le,[1:N+1]',N:-1:1); %//'
%// Chop-off at N x N length and reshape to have the final output
out = reshape(val(1:N*N),N,N);
Benchmarking
In this section we will cover runtime benchmarking for the various approaches listed on this page for the stated problem.
Benchmarking Code -
%datasizes = [10 20 50 70 100 200 500 700 1000]; %// Set -1
datasizes = [1000 2000 5000 7000 10000]; %// Set -2
fcns = {'repvecshiftdown_flipud_hankel','repvecshiftdown_toeplitz',...
'repvecshiftdown_repmat_bsxfun','repvecshiftdown_tril_toeplitz'};%//approaches
tsec = zeros(numel(fcns),numel(datasizes));
for k1 = 1:numel(datasizes),
A = randi(9,1,datasizes(k1)); %// Creare random input vector
for k2 = 1:numel(fcns), %// Time approaches
tsec(k2,k1) = timeit(#() feval(fcns{k2}, A), 1);
fprintf('\tFunction: %s (%3.2f sec)\n',fcns{k2},tsec(k2,k1));
end
end
figure; %% Plot Runtimes
plot(datasizes,tsec(1,:),'-rx'), hold on
plot(datasizes,tsec(2,:),'-bo')
plot(datasizes,tsec(3,:),'-k+')
plot(datasizes,tsec(4,:),'-g.')
set(gca,'xgrid','on'),set(gca,'ygrid','on'),
xlabel('Datasize (# elements)'), ylabel('Runtime (s)')
legend(upper(strrep(fcns,'_',' '))),title('Runtime')
Associated function codes (all approaches) -
function out = repvecshiftdown_repmat_bsxfun(A)
N = numel(A);
val = repmat([A(:);0],1,N).*bsxfun(#le,[1:N+1]',[N:-1:1]); %//'
out = reshape(val(1:N*N),N,N);
return;
function out = repvecshiftdown_tril_toeplitz(A)
out = tril( toeplitz(A) );
return;
function out = repvecshiftdown_toeplitz(A)
out = toeplitz(A, zeros(size(A)));
return;
function out = repvecshiftdown_flipud_hankel(A)
out = flipud( hankel( flipud(A(:)) ) );
return;
Runtime plots -
Set #1 [From 10 till 1000 datasizes]:
Set #2 [From 1000 till 10000 datasizes]:

How to find neighbors in 4D array in MATLAB?

I am a bit confused and would greatly appreciate some help.
I have read many posts about finding neighboring pixels, with this being extremely helpful:
http://blogs.mathworks.com/steve/2008/02/25/neighbor-indexing-2/
However I have trouble applying it on a 4D matrix (A) with size(A)=[8 340 340 15]. It represents 8 groups of 3D images (15 slices each) of which I want to get the neighbors.
I am not sure which size to use in order to calculate the offsets. This is the code I tried, but I think it is not working because the offsets should be adapted for 4 dimensions? How can I do it without a loop?
%A is a 4D matrix with 0 or 1 values
Aidx = find(A);
% loop here?
[~,M,~,~] =size(A);
neighbor_offsets = [-1, M, 1, -M]';
neighbors_idx = bsxfun(#plus, Aidx', neighbor_offsets(:));
neighbors = B(neighbors_idx);
Thanks,
ziggy
Have you considered using convn?
msk = [0 1 0; 1 0 1; 0 1 0];
msk4d = permute( msk, [3 1 2 4] ); % make it 1-3-3-1 mask
neighbors_idx = find( convn( A, msk4d, 'same' ) > 0 );
You might find conndef useful for defining the basic msk in a general way.
Not sure if I've understood your question but what about this sort of approach:
if you matrix is 1D:
M = rand(10,1);
N = M(k-1:k+1); %//immediate neighbours of k
However this could error if k is at the boundary. This is easy to fix using max and min:
N = M(max(k-1,1):min(k+1,size(M,1))
Now lets add a dimenion:
M = rand(10,10);
N = M(max(k1-1,1):min(k1+1,size(M,1), max(k2-1,1):min(k2+1,size(M,2))
That was easy, all you had to do was repeat the same index making the minor change of using size(M,2) for the boundary (and also I changed k to k1 and k2, you might find using an array for k instead of separate k1 and k2 variables works better i.e. k(1) and k(2))
OK so now lets skip to 4 dimensions:
M = rand(10,10,10,10);
N = M(max(k(1)-1,1):min(k(1)+1,size(M,1)), ...
max(k(2)-1,1):min(k(2)+1,size(M,2)), ...
max(k(3)-1,1):min(k(3)+1,size(M,3)), ...
max(k(4)-1,1):min(k(4)+1,size(M,4))); %// Also you can replace all the `size(M,i)` with `end` if you like
I know you said you didn't want a loop, but what about a really short loop just to refactor a bit and also make it generalized:
n=ndims(M);
ind{n} = 0;
for dim = 1:n
ind{dim} = max(k(dim)-1,1):min(k(dim)+1,size(M,dim));
end
N = M(ind{:});
Here's how to get the neighbors along the second dimension
sz = size( A );
ndims = numel(sz); % number of dimensions
[d{1:ndims}] = ind2sub( sz, find( A ) );
alongD = 2; % work along this dim
np = d{alongD} + 1;
sel = np <= sz( alongD ); % discard neighbors that fall outside image boundary
nm = d{alongD} - 1;
sel = sel & nm > 0; % discard neighbors that fall outside image boundary
d = cellfun( #(x) x(sel), d, 'uni', 0 );
neighbors = cat( 1, ...
ind2sub( sz, d{1:alongD-1}, np(sel), d{alongD+1:end} ),...
ind2sub( sz, d{1:alongD-1}, nm(sel), d{alongD+1:end} ) );

MeanShift Clustering on dataset

I have a numeric dataset and I want to cluster data with a non-parametric algorithm. Basically, I would like to cluster without specifying the number of clusters for the input. I am using this code that I accessed through the MathWorks File Exchange network which implements the Mean Shift algorithm. However, I don't Know how to adapt my data to this code as my dataset has dimensions 516 x 19.
function [clustCent,data2cluster,cluster2dataCell] =MeanShiftCluster(dataPts,bandWidth,plotFlag)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%perform MeanShift Clustering of data using a flat kernel
%
% ---INPUT---
% dataPts - input data, (numDim x numPts)
% bandWidth - is bandwidth parameter (scalar)
% plotFlag - display output if 2 or 3 D (logical)
% ---OUTPUT---
% clustCent - is locations of cluster centers (numDim x numClust)
% data2cluster - for every data point which cluster it belongs to (numPts)
% cluster2dataCell - for every cluster which points are in it (numClust)
%
% Bryan Feldman 02/24/06
% MeanShift first appears in
% K. Funkunaga and L.D. Hosteler, "The Estimation of the Gradient of a
% Density Function, with Applications in Pattern Recognition"
%*** Check input ****
if nargin < 2
error('no bandwidth specified')
end
if nargin < 3
plotFlag = true;
plotFlag = false;
end
%**** Initialize stuff ***
%[numPts,numDim] = size(dataPts);
[numDim,numPts] = size(dataPts);
numClust = 0;
bandSq = bandWidth^2;
initPtInds = 1:numPts
maxPos = max(dataPts,[],2); %biggest size in each dimension
minPos = min(dataPts,[],2); %smallest size in each dimension
boundBox = maxPos-minPos; %bounding box size
sizeSpace = norm(boundBox); %indicator of size of data space
stopThresh = 1e-3*bandWidth; %when mean has converged
clustCent = []; %center of clust
beenVisitedFlag = zeros(1,numPts,'uint8'); %track if a points been seen already
numInitPts = numPts %number of points to posibaly use as initilization points
clusterVotes = zeros(1,numPts,'uint16'); %used to resolve conflicts on cluster membership
while numInitPts
tempInd = ceil( (numInitPts-1e-6)*rand) %pick a random seed point
stInd = initPtInds(tempInd) %use this point as start of mean
myMean = dataPts(:,stInd); % intilize mean to this points location
myMembers = []; % points that will get added to this cluster
thisClusterVotes = zeros(1,numPts,'uint16'); %used to resolve conflicts on cluster membership
while 1 %loop untill convergence
sqDistToAll = sum((repmat(myMean,1,numPts) - dataPts).^2); %dist squared from mean to all points still active
inInds = find(sqDistToAll < bandSq); %points within bandWidth
thisClusterVotes(inInds) = thisClusterVotes(inInds)+1; %add a vote for all the in points belonging to this cluster
myOldMean = myMean; %save the old mean
myMean = mean(dataPts(:,inInds),2); %compute the new mean
myMembers = [myMembers inInds]; %add any point within bandWidth to the cluster
beenVisitedFlag(myMembers) = 1; %mark that these points have been visited
%*** plot stuff ****
if plotFlag
figure(12345),clf,hold on
if numDim == 2
plot(dataPts(1,:),dataPts(2,:),'.')
plot(dataPts(1,myMembers),dataPts(2,myMembers),'ys')
plot(myMean(1),myMean(2),'go')
plot(myOldMean(1),myOldMean(2),'rd')
pause
end
end
%**** if mean doesnt move much stop this cluster ***
if norm(myMean-myOldMean) < stopThresh
%check for merge posibilities
mergeWith = 0;
for cN = 1:numClust
distToOther = norm(myMean-clustCent(:,cN)); %distance from posible new clust max to old clust max
if distToOther < bandWidth/2 %if its within bandwidth/2 merge new and old
mergeWith = cN;
break;
end
end
if mergeWith > 0 % something to merge
clustCent(:,mergeWith) = 0.5*(myMean+clustCent(:,mergeWith)); %record the max as the mean of the two merged (I know biased twoards new ones)
%clustMembsCell{mergeWith} = unique([clustMembsCell{mergeWith} myMembers]); %record which points inside
clusterVotes(mergeWith,:) = clusterVotes(mergeWith,:) + thisClusterVotes; %add these votes to the merged cluster
else %its a new cluster
numClust = numClust+1 %increment clusters
clustCent(:,numClust) = myMean; %record the mean
%clustMembsCell{numClust} = myMembers; %store my members
clusterVotes(numClust,:) = thisClusterVotes;
end
break;
end
end
initPtInds = find(beenVisitedFlag == 0); %we can initialize with any of the points not yet visited
numInitPts = length(initPtInds); %number of active points in set
end
[val,data2cluster] = max(clusterVotes,[],1); %a point belongs to the cluster with the most votes
%*** If they want the cluster2data cell find it for them
if nargout > 2
cluster2dataCell = cell(numClust,1);
for cN = 1:numClust
myMembers = find(data2cluster == cN);
cluster2dataCell{cN} = myMembers;
end
end
This is the test code I am using to try and get the Mean Shift program to work:
clear
profile on
nPtsPerClust = 250;
nClust = 3;
totalNumPts = nPtsPerClust*nClust;
m(:,1) = [1 1];
m(:,2) = [-1 -1];
m(:,3) = [1 -1];
var = .6;
bandwidth = .75;
clustMed = [];
%clustCent;
x = var*randn(2,nPtsPerClust*nClust);
%*** build the point set
for i = 1:nClust
x(:,1+(i-1)*nPtsPerClust:(i)*nPtsPerClust) = x(:,1+(i-1)*nPtsPerClust:(i)*nPtsPerClust) + repmat(m(:,i),1,nPtsPerClust);
end
tic
[clustCent,point2cluster,clustMembsCell] = MeanShiftCluster(x,bandwidth);
toc
numClust = length(clustMembsCell)
figure(10),clf,hold on
cVec = 'bgrcmykbgrcmykbgrcmykbgrcmyk';%, cVec = [cVec cVec];
for k = 1:min(numClust,length(cVec))
myMembers = clustMembsCell{k};
myClustCen = clustCent(:,k);
plot(x(1,myMembers),x(2,myMembers),[cVec(k) '.'])
plot(myClustCen(1),myClustCen(2),'o','MarkerEdgeColor','k','MarkerFaceColor',cVec(k), 'MarkerSize',10)
end
title(['no shifting, numClust:' int2str(numClust)])
The test script generates random data X. In my case. I want to use the matrix D of size 516 x 19 but I am not sure how to adapt my data to this function. The function is returning results that are not agreeing with my understanding of the algorithm.
Does anyone know how to do this?