%
% Demo of linear regression on simple generated dataset
%

% Fitting a least-squares linear regression is easily accomplished in MATLAB
% using the backslash operator: '\'.
clear all;

% generate 1000 5 dimensional sample voxels
X = rand(1e3,5);
% specify coefficients of a linear function
beta = [-1 2 -3 4 -5 6]';
% generate the output data using those coefficients + some random noise
Y = beta(1) + X * beta(2:end) + rand(1000,1)*0.01;
% append a column of ones and use the backslash operator to determine the
% coefficients
X = [ones(size(X,1),1) X];
% get the coefficients
betaout = X \ Y
% get the coefficients using statists toolbox
betaout_stat = regress(Y,X)

error = sum((betaout-beta).^2)
error_stat = sum((betaout_stat-beta).^2)

return