function sheet06

% Generate some data
N = 100;
M1 = [1; 2];
M2 = [-5; 7];
X = [randn(2, N) + repmat(M1, 1, N), ...
randn(2, N) + repmat(M2, 1, N)];
Y = [ones(1, N), -ones(1, N)];

% Show data
subplot(3,1,1);
plotdata(X, Y);

% Compute fda direction and another one and show
% the projected data
W = fda(X, Y)
W1 = [1; 0];

hold on;
plotdata(project(X, W), Y);
plotdata(project(X, W1), Y);
hold off;

% Histograms for the fda direction
subplot(3,1,2);
show_projected_data(W'*X, Y);

% Histograms for the other direction
subplot(3,1,3);
show_projected_data(W1'*X, Y);

% Plot two classes.
function plotdata(X, Y)
P = (Y == 1);
N = (Y == -1);

plot(X(1, P), X(2, P), 'r+', X(1, N), X(2, N), 'bo');

axis equal
grid on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your solutions below
%

% 1. Return scatter matrix S = (X - M)(X - M)'
function S = scatter(X, M)
% ...

% 2. Compute FDA direction.
function W = fda(X, Y)
% ...

% 3. Project X to subspace spanned by W.
function X = project(X, W)
% ...

% 4. Plot histograms for both classes of a
% one-dimensional data set X.
function show_projected_data(X, Y)
% ...