Splitting a matrix into group based into the first column and select the maximum value from the second column for each interval

2 vues (au cours des 30 derniers jours)
I have a big matrix,x, with size of 21,6000 x 2. The first Column is A and The second one is B. I'm looking for away to split the first column into interval and find the maximum value from each interval in B. For example;
if true
Original Matrix =
0.468490416082415 -68.0623566640544
0.861091227948802 -62.0460361812610
0.920662009016715 -52.2025156248263
1.08087425638995 -34.5197924821680
1.17439664631856 -48.4780486951264
1.21018219911556 -45.0147047153047
1.24281651424282 -58.9951529447540
1.31409003462488 -62.4048732765717
1.32139564340592 -45.4143879292352
1.32229841475997 -52.0082496386087
1.40064375002948 -49.6318599064140
1.44367554410444 -58.8869137147488
1.45458058653115 -55.1869654737137
1.46737972112234 -49.2348443830192
1.47623284910443 -41.3789920515417
1.48211171204936 -44.6901137312740
1.48464667747544 -45.5836110933174
1.50221080043184 -60.8266330903523
1.51770839240522 -45.3482744837045
1.53398235212043 -45.6985828174899
end
After splitting them into 5 group, the first group will look like
if true
0.468490416082415 -68.0623566640544
0.861091227948802 -62.0460361812610
0.920662009016715 -52.2025156248263
1.08087425638995 -34.5197924821680
1.17439664631856 -48.4780486951264
end
From this first group, I would select the maximum in B and create a new vector which has:
if true
1.08087425638995 -34.5197924821680
end
I tried this code but did not work for me.
if true
d1 = sortrows(x,[1 2])
subs=discretize(d1(:,1),3)
intervalMax=accumarray(subs(:),d1(:,2),[],@max)
end
Can anyone please help. thank you in advance.

Réponse acceptée

Bruno Luong
Bruno Luong le 17 Oct 2018
Modifié(e) : Bruno Luong le 17 Oct 2018
AB = [...
0.468490416082415 -68.0623566640544
0.861091227948802 -62.0460361812610
0.920662009016715 -52.2025156248263
1.08087425638995 -34.5197924821680
1.17439664631856 -48.4780486951264
1.21018219911556 -45.0147047153047
1.24281651424282 -58.9951529447540
1.31409003462488 -62.4048732765717
1.32139564340592 -45.4143879292352
1.32229841475997 -52.0082496386087
1.40064375002948 -49.6318599064140
1.44367554410444 -58.8869137147488
1.45458058653115 -55.1869654737137
1.46737972112234 -49.2348443830192
1.47623284910443 -41.3789920515417
1.48211171204936 -44.6901137312740
1.48464667747544 -45.5836110933174
1.50221080043184 -60.8266330903523
1.51770839240522 -45.3482744837045
1.53398235212043 -45.6985828174899 ]
B = reshape(AB(:,2),5,[]);
[~,loc] = max(B,[],1);
Amax = AB(loc+5*(0:size(B,2)-1),:)
Result
Amax =
1.0809 -34.5198
1.2102 -45.0147
1.4762 -41.3790
1.4821 -44.6901
  2 commentaires
Yaser Khojah
Yaser Khojah le 17 Oct 2018
Thanks Bruno for your help. I have tried your code with my data and i'm wondering if I can control the size of the interval.
Bruno Luong
Bruno Luong le 17 Oct 2018
The size of interval is 5, so change the two "5" in my code.
p = 5;
B = reshape(AB(:,2),p,[]);
[~,loc] = max(B,[],1);
Amax = AB(loc+p*(0:size(B,2)-1),:)
If the length of AB is not divisible by the block size (p) you must Pad AB by NaN like this
AB(end+1:ceil(size(AB,1)/p)*p,2) = NaN;
before the rest of the code.

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 17 Oct 2018
You could use MAT2TILES (Download) as follows
C=mat2tiles(OriginalMatrix, [5,2]);
for i=1:numel(C)
A=C{i}(:,1);
B=C{i}(:,2);
[~,j]=max(B);
C{i}=[A(j), B(j)];
end
intervalMax=cell2mat(C);

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by