

rng(1337);

%class 1
spread1_1 = randn([1000 1]);
spread1_2 = randn([1000 1]);
feat_class1(1:1000,1) = 0.1 + 0.2 * spread1_1 - 0.2 * spread1_2;
feat_class1(1:1000,2) = 0.1 + 0.2 * spread1_1 + 0.1 * spread1_2;
% class 2
spread2_1 = randn([1000 1]);
spread2_2 = 0.5 * randn([1000 1]);
feat_class2(1:1000,1) = 0.7 + 0.2 * spread2_1 - 0.2 * spread2_2;
feat_class2(1:1000,2) = 0.7 + 0.15 * spread2_1 + 0.15 * spread2_2;

feats = cat(1,feat_class1,feat_class2);
classes = cat(1,zeros([size(feat_class1,1) 1]),ones([size(feat_class1,1) 1]));

% train the forest!
BaggedEnsemble = TreeBagger(50,feats,classes);

% sample forest output
[X,Y] = meshgrid(0:0.01:1,0:0.01:1);
X = X(:);
Y = Y(:);
sample(:,1) = X;
sample(:,2) = 1-Y;
[res,scores] = predict(BaggedEnsemble,sample);
resn = scores(:,1);
resn = reshape(resn,[101 101]);


% plot
figure;
imshow(resn);
hold on;
plot(feat_class1(:,1)*101,101-feat_class1(:,2)*101,'b*');
plot(feat_class2(:,1)*101,101-feat_class2(:,2)*101,'r*');