view general/funutils/groupby.m @ 6:0ce3c2070089

Removed duplicate code and fixed doc in timed_action.
author samer
date Mon, 14 Jan 2013 14:33:37 +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