annotate general/funutils/groupby.m @ 4:e44f49929e56
Adding reorganised general toolbox, now in several subdirectories.
author |
samer |
date |
Sat, 12 Jan 2013 19:21:22 +0000 |
parents |
|
children |
beb8a3f4a345 |
rev |
line source |
samer@4
|
1 function y=groupby_or(f,x)
|
samer@4
|
2 % groupby_or - collect rows of x into equivalence classes based f
|
samer@4
|
3 %
|
samer@4
|
4 % groupby_or :: ([[1,M]]->A), [[N,M]] -> {[K]->[[L,M]]} :- equality(A).
|
samer@4
|
5 %
|
samer@4
|
6 % IMPORTANT: this version assumes that data is ordered by f, that is,
|
samer@4
|
7 % only consecutive rows will be grouped together.
|
samer@4
|
8
|
samer@4
|
9 N=size(x,1);
|
samer@4
|
10 y={};
|
samer@4
|
11
|
samer@4
|
12 i=1; j=1;
|
samer@4
|
13 while j<=N
|
samer@4
|
14 if i==j, z=f(x(i,:)); end
|
samer@4
|
15 if j==N || f(x(j+1,:)~=z)
|
samer@4
|
16 y=vertcat(y,{x(i:j,:)});
|
samer@4
|
17 i=j+1; j=i;
|
samer@4
|
18 else j=j+1;
|
samer@4
|
19 end
|
samer@4
|
20 end
|
samer@4
|
21
|