# try to find max and min of x and y for each cluster

3 views (last 30 days)
zheng on 29 Mar 2011
Hi, all
In my case, I have a table which stores x,y,and cluster numer in three columns. example as below
===================================================
608654.966062901 4820462.57604139 1
608662.024953254 4820455.91371599 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 3
608676.221786682 4820442.70145371 3
608724.787042597 4820393.49671617 3
608732.086699384 4820386.94944425 3
608739.632386523 4820380.18398164 3
608747.011082827 4820373.74909310 3
608754.551408620 4820367.31682751 3
=====================================================
I am trying to find min/max of x and min/max of y in each cluster, and save them into a new table 'statcluster', as below
===============================================================
minX maxX minY maxY clusternum
===============================================================
************
Here is code I wrote, but it cannot work properly. some answers correct, but others are wrong. Please comment on it. Thanks alot
************
% find number of cluster -- cn
cn=max(dataa(:,3));
row=length(dataa(:,1));
% statcluster save max/min of x and max/min of y and their corresponding
% cluster number
statcluster=zeros(cn,5);
% compute number of elements in each cluster
trash=histc(dataa(:,3),unique(dataa(:,3)));
for i=1:row
for j=1:cn
if dataa(i,3)==j
% min/max of x
statcluster(j,1)=min(dataa(i,1));
statcluster(j,2)=max(dataa(i,1));
% min/max of y
statcluster(j,3)=min(dataa(i,2));
statcluster(j,4)=max(dataa(i,2));
% cluster number
statcluster(j,5)=j;
break
end
end
end

Matt Fig on 29 Mar 2011
One way to do this would be to use the ACCUMARRAY function.
DAT = [608654.966062901 4820462.57604139 1
608662.024953254 4820455.91371599 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 1
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 2
608676.221786682 4820442.70145371 3
608676.221786682 4820442.70145371 3
608724.787042597 4820393.49671617 3
608732.086699384 4820386.94944425 3
608739.632386523 4820380.18398164 3
608747.011082827 4820373.74909310 3
608754.551408620 4820367.31682751 3]
MX_x = accumarray(DAT(:,3),DAT(:,1) ,[], @max);
MN_x = accumarray(DAT(:,3),DAT(:,1) ,[], @min);
MX_y = accumarray(DAT(:,3),DAT(:,2) ,[], @max);
MN_y = accumarray(DAT(:,3),DAT(:,2) ,[], @min);
statcluster = [MN_x MX_x MN_y MX_y (1:3).']
zheng on 29 Mar 2011
appreciate