an issue with Matrix Index exceed Matrix
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have changed the code to:
X=data;
epsilon=1;
MinPts=37;
C=0;
[IDX]=DBSCANf(X,epsilon,MinPts);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [IDX, isnoise]=DBSCANf(X,epsilon,MinPts)
C=0;
n=size(X,1);
% cluster lables initialize
% IDX=zeros(n,1);
D=pdist2(X,X);
visited=false(n,1); % produce array of logical false for example, false(2)
logical 2x2 zero array
isnoise=false(n,1); % array of logical false
for i=1:n
if ~visited(i)
visited(i)=true;
Neighbors=RegionQuery(i,D,epsilon);
if numel(Neighbors) < MinPts % Number of array elements
% X(i,:) is NOISE
isnoise(i)=true;
else
C=C+1;
IDX= ExpandCluster(i,Neighbors,C,visited,D,epsilon,MinPts,n);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function IDX = ExpandCluster(i,Neighbors,C,visited,D,epsilon,MinPts,n)
IDX=zeros(n,1);
IDX(i)=C;
k = 1;
while true
j = Neighbors(k);
if ~visited(j)
visited(j)=true;
Neighbors2=RegionQuery(j,D,epsilon);
if numel(Neighbors2)>=MinPts
Neighbors=[Neighbors Neighbors2]; %#ok
end
end
if IDX(j)==0
IDX(j)=C;
end
k = k + 1;
if k > numel(Neighbors)
break;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Neighbors=RegionQuery(i,D,epsilon)
Neighbors=find(D(i,:)<=epsilon); % distance will work one by one i=1 to 10
end
the IDX values are zeros :( ??
0 Commenti
Risposte (1)
dpb
il 24 Set 2017
Modificato: dpb
il 24 Set 2017
Use the debugger to see where your implementation logic breaks down...but, the problem arises from
function IDX =ExpandCluster(i,Neighbors,C,visited,D,epsilon,MinPts)
IDX(i)=C;
k = 1;
while true
j = Neighbors(k);
...
if IDX(j)==0
...
the array IDX will be of size(1,i) and the first time the function is called i==1 but then j isn't directly related to i.
functions have local scope, array IDX inside the function has nothing whatever to to with the array in the calling function; if you intend to be operating on it you'll need to pass it.
Also note that Matlab passes only copy of arrays; any modifications made in the function are again local copies only and array arguments modified not passed back to the calling routine.
Upshot is, allocate IDX in the function if you're building it in the function.
Also your assignment in the calling function will overwrite the previous array definition and IDX will be size as returned from the ExpandCluster function. As you've written logic here the statement
IDX=zeros(n,1);
has no effect at all.
5 Commenti
dpb
il 26 Set 2017
Well, we don't either...it's logic error, not syntax and we don't have your data nor even an explanation of what you are trying to do. That's the first part of coding; writing a clear problem statement and defining the data structure so one can implement it in code.
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!