view general/funutils/groupby.m @ 27:5de03f77dae1

Added documentation about types and revised arrow type specifications.
author samer
date Sat, 19 Jan 2013 14:22:09 +0000
parents e44f49929e56
children beb8a3f4a345
line wrap: on
line source
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