MATLAB: Find the median of sequential duplicate entries

referenceunique

Hi! So I have a matrix
num =
1 4
2 4
3 4
4 4
5 5
6 6
7 6
8 6
9 6
10 6
11 7
....
31 14
32 14
33 13
34 12
35 12
36 12
37 12
38 12
39 9
40 9
41 9
42 9
43 8
44 8
45 8
46 8
47 5
48 5
49 4
50 3
And I want to take the median of the x entries with sequential y entries that are duplicate such that
inum =
2.5 4
5 5
8 6
11 7
....
31.5 14
33 13
36 12
40.5 9
44.5 8
47.5 5
49 4
50 3
I am using unique such that
[C,ia,ic] = unique(num(:,2),'stable')
OutMatrix = [accumarray(ic, num(:,1), [], @median ) ,C];
However, my outmatrix is showing the median of all the x entries that have y values that are the same i.e It took the median of 1,2,3,4,49 and not the median on 1,2,3,4 and 49 How do I do this? Thanks

Best Answer

One way to achieve this is to replace num(:,2) with a monotonically increasing group/block ID that you build. Here is how:
blockStart = [true; diff(num(:,2)) ~= 0] ;
blockId = cumsum( blockStart ) ;
applied to the numbers that you gave, this builds the 3rd column below;
>> [num, blockId]
ans =
1 4 1
2 4 1
3 4 1
4 4 1
5 5 2
6 6 3
7 6 3
8 6 3
9 6 3
10 6 3
11 7 4
31 14 5
32 14 5
33 13 6
34 12 7
35 12 7
36 12 7
37 12 7
38 12 7
39 9 8
40 9 8
41 9 8
42 9 8
43 8 9
44 8 9
45 8 9
46 8 9
47 5 10
48 5 10
49 4 11
50 3 12
and you can see that, using this, you eliminate the interference between blocks with same num(:,2) on both sides of the peak. Then you perform the call to ACCUMARRAY that you already implemented, using blockId this time:
>> OutMatrix = [accumarray( blockId, num(:,1), [], @median ), ...
num(blockStart,2)]
OutMatrix =
2.5000 4.0000
5.0000 5.0000
8.0000 6.0000
11.0000 7.0000
31.5000 14.0000
33.0000 13.0000
36.0000 12.0000
40.5000 9.0000
44.5000 8.0000
47.5000 5.0000
49.0000 4.0000
50.0000 3.0000