Sorting by number groups, Need help makeing a better function...

4 vues (au cours des 30 derniers jours)
Chris E.
Chris E. le 30 Déc 2012
I'm sorting data by number groups and need some help on how to do this properly and efficiently.
%Here are the inputs
WPL = [1.0000 0.2326;
5.0000 0.1217;
4.0000 0.0881;
6.0000 0.0842;
8.0000 0.0764;
11.0000 0.0549;
13.0000 0.0508;
3.0000 0.0460;
10.0000 0.0412;
12.0000 0.0359;
9.0000 0.0298;
15.0000 0.0271;
18.0000 0.0243;
2.0000 0.0189;
19.0000 0.0147;
7.0000 0.0119;
16.0000 0.0096;
21.0000 0.0084;
14.0000 0.0061;
20.0000 0.0042;
17.0000 0.0037;
27.0000 0.0027;
23.0000 0.0020;
24.0000 0.0017;
25.0000 0.0013;
22.0000 0.0010;
26.0000 0.0006;
28.0000 0;
29.0000 0;
30.0000 0;
31.0000 0;
32.0000 0;
33.0000 0;
34.0000 0;
35.0000 0;
36.0000 0;
37.0000 0;
38.0000 0;
39.0000 0];
NG = [1.0000 0.4834;
6.0000 0.2533;
11.0000 0.1796;
16.0000 0.0671;
21.0000 0.0127;
26.0000 0.0038;
31.0000 0;
36.0000 0];
%here is the code
ng1 = (1 <= WPL(:,1) & WPL(:,1) <= 5)*1;
ng2 = (6 <= WPL(:,1) & WPL(:,1) <= 10)*6;
ng3 = (11 <= WPL(:,1) & WPL(:,1) <= 15)*11;
ng4 = (16 <= WPL(:,1) & WPL(:,1) <= 20)*16;
ng5 = (21 <= WPL(:,1) & WPL(:,1) <= 25)*21;
ng6 = (26 <= WPL(:,1) & WPL(:,1) <= 30)*26;
ng7 = (31 <= WPL(:,1) & WPL(:,1) <= 35)*31;
ng8 = (36 <= WPL(:,1) & WPL(:,1) <= 39)*36;
numgrp = ng1+ng2+ng3+ng4+ng5+ng6+ng7+ng8;
for n = 1:size(numgrp,1)
if numgrp(n) == 1
List(n) = NG(1,2)*WPL(n,2);
elseif numgrp(n) == 6
List(n) = NG(2,2)*WPL(n,2);
elseif numgrp(n) == 11
List(n) = NG(3,2)*WPL(n,2);
elseif numgrp(n) == 16
List(n) = NG(4,2)*WPL(n,2);
elseif numgrp(n) == 21
List(n) = NG(5,2)*WPL(n,2);
elseif numgrp(n) == 26
List(n) = NG(6,2)*WPL(n,2);
elseif numgrp(n) == 31
List(n) = NG(7,2)*WPL(n,2);
elseif numgrp(n) == 36
List(n) = NG(8,2)*WPL(n,2);
end
end
WPL = sortrows([WPL(:,1) (List/sum(List))'],-2);
Here is the output, this output works, however I would like to improve this code to make such an output, any help is appreciated!!!
WPL =
1.0000 0.3285
5.0000 0.1719
4.0000 0.1244
3.0000 0.0650
6.0000 0.0623
8.0000 0.0565
10.0000 0.0305
11.0000 0.0288
2.0000 0.0267
13.0000 0.0267
9.0000 0.0221
12.0000 0.0188
15.0000 0.0142
7.0000 0.0088
18.0000 0.0048
14.0000 0.0032
19.0000 0.0029
16.0000 0.0019
20.0000 0.0008
17.0000 0.0007
21.0000 0.0003
23.0000 0.0001
24.0000 0.0001
25.0000 0.0000
22.0000 0.0000
27.0000 0.0000
26.0000 0.0000
28.0000 0
29.0000 0
30.0000 0
31.0000 0
32.0000 0
33.0000 0
34.0000 0
35.0000 0
36.0000 0
37.0000 0
38.0000 0
39.0000 0

Réponse acceptée

Roger Stafford
Roger Stafford le 31 Déc 2012
List = NG(floor((WPL(:,1)-1)/5)+1,2).*WPL(:,2);
WPL = sortrows([WPL(:,1),List/sum(List)],-2);
You may be unhappy with this solution. It takes advantage of the regular intervals of values in NG(:,1) and the corresponding groupings of values in WPL(:,1) which you have exhibited here. To make it more general the first step should probably be to call on 'histc' in an appropriate way.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 31 Déc 2012
Hint to get you started:
[counts, binnum] = histc(WPL(:,1), [NG(2:end,1];inf]);
numgrp = NG(binnum(:), 1);

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by