function [splits,splitvalues,notinanysplit] = makesplits (x,values) % MAKESPLITS Partitions indices of a vector by their values % splits = makesplits (x,[values]) % % x is a n-element vector % values can be a cellarray or integer % % if values is a cell array, then % splits{k} has all indices j such that x(j) is in splitvalues{k} = values{k} % splitvalues = values % notinanysplit has indices with all j such that x(j) is not in any values{i}. % % If values is an integer k % splits{k} has all indices j such that x(j) is in splitvalues{k} % splitvalues is a k-element cell array representing the split created by makesplits. % The values are split so that when concatenated, they are sort(unique(x)), and % so the first k-1 splits are equal-sized. % notinanysplit = [] % % If values is not supplied, it is assumed to be length(unique(x)). % Dinoj Surendran dinoj@cs.uchicago.edu 12 Jun 05 iscol=1;% if x is a column vector if (1==size(x,1)) iscol=0;% if x is a row vector end ux = sort(unique(x)); if nargin<2, values=length(ux); end if isnumeric(values) k=values; lux = length(ux); a=ceil(lux/k); a_st = 1+a*[0:k-1]; a_en = min(a*[1:k],lux); for i=1:k splitvalues{i} = [ux(a_st(i):a_en(i))]; splits{i} = find(x>=ux(a_st(i)) & x<=ux(a_en(i))); end notinanysplit = []; else notinanysplit = [1:length(x)]; for j=1:length(values) splits{j} = []; for k=1:length(values{j}) fi = find(x==values{j}(k)); notinanysplit = setdiff(notinanysplit,fi); if iscol splits{j} = vertcat(splits{j},fi); else splits{j} = horzcat(splits{j},fi); end end end splitvalues = values; end