function [discretized] = eats(data)

# Modified Fayyad-Irani discretization using variance instead of entropy

# Set Up
[rows, cols] = size(data);

#E(A,T,S) = |S1|/|S|var(S1) + |S2|/|S|var(S2)
MinEats = sum(var(data));
Split = 0;
for i = 1 : rows - 1
	ats = ((i / rows)*(sum(var(data(1:i,:))))) + (((rows - i) / rows)*(sum(var(data(i+1:end,:)))));
	if (ats < MinEats)
		Split = i;
		MinEats = ats;
	end
end

# Determine gain for stopping rules
# Delta(A,T,S) = lg(3^k - 2) - [k * Var(S) - k1 Var(S1) - k2 Var(S2)]
# where k = number of class labels
# Gain(A,T,S) = Var(S) - E(A,T,S)
# If Gain(A,T,S) < lg(N-1) / N + delta(a,t,s)/N
# where N is the number of instances in S
# Because this is recursive, s refers to the subset
if (Split > 0)
	GainATS = sum(var(data)) - MinEats;
	DeltaATS = sum(var(data)) + sum(var(data(1:Split,:))) + sum(var(data(Split+1:end,:)));
	# Threshold = ((log((rows * cols) - 1)/log(2))/(rows*cols)) + DeltaATS/(rows*cols);
	# The outlined threshold was not working well for data given, so rule was switched to 10% of delta
	Threshold = .10*DeltaATS;
	if (GainATS > Threshold)
		discretized = [eats(data(1:Split,:));eats(data(Split+1:end,:));];
	else
		# Done, tag and return
		Set1 = data(1:Split,:);
		Set1(:,cols+1) = sum(var(Set1));
		Set2 = data(Split+1:end,:);
		Set2(:,cols+1) = sum(var(Set2));
		discretized = [Set1;Set2;];
	end
else
	# Done splitting, tag and return
	discretized = data;
	discretized(:,cols+1) = sum(var(data));
end