Improve Growing Cell Array to Cluster Points

1 view (last 30 days)
jmmmrd
jmmmrd on 29 Apr 2014
Edited: jmmmrd on 29 Apr 2014
Hi! I would like to know how to efficiently run this part of my progam. Basically for a background of what I aim to do, I want to cluster points. There are around 10,000 points. These points have computed "forces" between them that is stored in matrix F. So the force between point 1 and point 2 is F(1,2). I would then like to "cluster points" with sufficient F acting on them (Force setting/threshold), that is 2 points with sufficient F between them belong to the same cluster. I have a code as seen below. A cell array CLUSTER was made to contain the cluster assignments. So CLUSTER{i} is the cell containing the ith cluster with clustered points in it. However, for some F settings, the implementation takes forever. I have read about preallocation and parfor (parfor can't be done since there is dependency in iterations). But does preallocation of cell arrays mean that individual cells are not preallocated with memory? Is there any other way around this? Profiling tells me that ismember has the biggest share in computing. I hope to improve the code with your suggestions. Thanks a lot!
CLUSTER = {};
for fi = 1:srow
for fj = 1:scol
if fj > fi % to eliminate redundancy, diagonal mirror elements of F !check on this
if F(fi,fj) >= 2000 % Force setting
if( (~ismember(1,cellfun(@(x)ismember(fi,x),CLUSTER))) && (~ismember(1,cellfun(@(x)ismember(fj,x),CLUSTER))) ) % fi & fj are not in CLUSTER
CLUSTER{end+1} = [fi fj];
end
%%if( (ismember(1,cellfun(@(x)ismember(fi,x),CLUSTER))) && (ismember(1,cellfun(@(x)ismember(fj,x),CLUSTER))) ) % fi & fj are in CLUSTER
%do nothing since lfi and fj are in CLUSTER
%%end
if( (ismember(1,cellfun(@(x)ismember(fi,x),CLUSTER))) && (~ismember(1,cellfun(@(x)ismember(fj,x),CLUSTER))) ) % fi in CLUSTER, fj not in CLUSTER
c = find(cellfun(@(x)ismember(fi,x),CLUSTER));
CLUSTER{c} = [CLUSTER{c} fj];
end
if( (~ismember(1,cellfun(@(x)ismember(fi,x),CLUSTER))) && (ismember(1,cellfun(@(x)ismember(fj,x),CLUSTER))) ) % fi not in CLUSTER, fj in CLUSTER
c = find(cellfun(@(x)ismember(fj,x),CLUSTER));
CLUSTER{c} = [CLUSTER{c} fi];
end
end
end
end
end

Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!