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 :( ??

답변 (1개)

dpb
dpb 2017년 9월 24일
편집: dpb 2017년 9월 24일

0 개 추천

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

shawin
shawin 2017년 9월 25일
편집: shawin 2017년 9월 25일
I have changed the code, but iam getting zeros IDX values??
dpb
dpb 2017년 9월 25일
Well, that would appear to be a logic error in that the ExpandCluster function isn't working as you expect.
Use the debugger and work through the logic to see where it isn't doing what you intend...the structure looks ok to return the IDX array; now you've got to work on filling it with what is needed.
shawin
shawin 2017년 9월 26일
@dpb: I do not know where is the error?
dpb
dpb 2017년 9월 26일
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.
shawin
shawin 2017년 9월 26일
편집: shawin 2017년 9월 26일
dpb: the algorithm is working well if we have it in the format below : https://pastebin.com/17DgD4EU open link but when separate the method it generates zeros

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2017년 9월 24일

편집:

2017년 9월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by