Mercurial > hg > ishara
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general/funutils/groupby.m Sat Jan 12 19:21:22 2013 +0000 @@ -0,0 +1,21 @@ +function y=groupby_or(f,x) +% groupby_or - collect rows of x into equivalence classes based f +% +% groupby_or :: ([[1,M]]->A), [[N,M]] -> {[K]->[[L,M]]} :- equality(A). +% +% IMPORTANT: this version assumes that data is ordered by f, that is, +% only consecutive rows will be grouped together. + + N=size(x,1); + y={}; + + i=1; j=1; + while j<=N + if i==j, z=f(x(i,:)); end + if j==N || f(x(j+1,:)~=z) + y=vertcat(y,{x(i:j,:)}); + i=j+1; j=i; + else j=j+1; + end + end +