try to find max and min of x and y for each cluster
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Matt Fig
am 29 Mär. 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).']
4 Kommentare
Matt Fig
am 29 Mär. 2011
Did you copy and paste my code exactly? Because I just did and got no horzcat error. I wonder if you are using data that is different than what I pasted above. Perhaps there are more than 3 groups in your data???
If not, try to copy EVERYTHING in the code I wrote above and paste, it should work. Then you need to figure out what is different about your data.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!