How do I group a sample data by a column into small groups?

I have a sample dataset of the form below and I want to sort into group of A = 11 - 20, B = 21 - 30, C = 31 - 40, etc. using the second column to do the sorting. I tried this
N = sortrows(N,2);
if N(:,2)<=0;
N0 = N;
elseif (N(:,2)>=1) & (N(:,2)<=10);
N10 = N;
elseif (N(:,2)>10) & (N(:,2)<=20);
N20 = N;
elseif (N(:,2)>20) & (N(:,2)<=30);
N30 = N;
elseif (N(:,2)>30) & (N(:,2)<=40);
N40 = N;
if N(:,2) > 40
N50 = N50(N);
end
but nothing was displayed
DATASET
16.07 88.9 1007
15.91 89.1 1007
19.87 67.48 1008
20.45 61.71 1008
21.62 53.97 1009
22.02 47.95 1009
18.54 82.9 1006
18.66 82 1006
18.46 84.1 1006
18.36 83.6 1006
24.38 54.33 1008
25.18 50.33 1008
25.9 46.73 1008
26.46 45.79 1008
30.29 31.58 1006
30.33 29.83 1006
31.76 24.36 1004
32.37 24.63 1004
32.38 26.74 1004
32.23 24.22 1004

 Risposta accettata

>> edges=[-inf 1 10:10:40 inf]; % set up bin edges
>> [n,bin]=histc(dat(:,2),edges) % count, bin...
>> for i=unique(bin).',dat(bin==i,2),end % display results
ans =
29.8300
24.3600
24.6300
26.7400
24.2200
ans =
31.5800
ans =
88.9000
89.1000
67.4800
61.7100
53.9700
47.9500
82.9000
82.0000
84.1000
83.6000
54.3300
50.3300
46.7300
45.7900

7 Commenti

Thank you dpb.
However, I need the answers to include the other columns' corresponding value. Also, each of the answers in a separate file.
Many thanks and kind regards,
Purely demonstrative of the binning operation; do what you want with the data inside the loop...address whatever set/subset you wish; I just did the 2nd column for clarity that it did the binning as desired.
>> for i=unique(bin).',dat(bin==i,:),end
ans =
1.0e+03 *
0.0303 0.0298 1.0060
0.0318 0.0244 1.0040
0.0324 0.0246 1.0040
0.0324 0.0267 1.0040
0.0322 0.0242 1.0040
ans =
1.0e+03 *
0.0303 0.0316 1.0060
ans =
1.0e+03 *
0.0161 0.0889 1.0070
0.0159 0.0891 1.0070
0.0199 0.0675 1.0080
0.0204 0.0617 1.0080
0.0216 0.0540 1.0090
0.0220 0.0480 1.0090
0.0185 0.0829 1.0060
0.0187 0.0820 1.0060
0.0185 0.0841 1.0060
0.0184 0.0836 1.0060
0.0244 0.0543 1.0080
0.0252 0.0503 1.0080
0.0259 0.0467 1.0080
0.0265 0.0458 1.0080
>>
Yes dpb. I need you to specify the subsets within the loop.
Many thanks.
dpb
dpb il 10 Giu 2015
Modificato: dpb il 10 Giu 2015
They're there, one each loop shows up as ans. Put whatever work you need to do on each set within the loop at that point; since you don't give any further information what that should be is unknown to us. As shown above, there's the full subset; the only difference is in the indexing of the second dimension to address the full row (":") instead of ("2").
What more is there?
The specified subsets are A, B, C, etc. Hence each ans ought to be A, B, C, etc. because only the last ans is retrievable.
Many thanks.
As I said, put what you want inside the loop...
"...each ans ought to be A, B, C,..."
Do NOT do this...for reasons and alternatives see the FAQ How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop?
j=0; % initialize counter for cell array
for i=unique(bin).'
j=j+1; % increment counter
a{j}=dat(bin==i,:); % store this set
end
Thank you very much dpb! You are absolutely correct.

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by