% generate two data sets:
n = 200;
d = 0.2;
x = randn(n, 1);
y = randn(n, 1) + d;

% difference of sample means:
md = mean(y) - mean(x);
fprintf('difference of means of data d = %.2f\n', md);

% distribution of null hypothesis by permutation:
nperm = 1000;
xy = [x; y];                         % x and y data in one column vector
ds = zeros(nperm,1);
for i = 1:nperm
    xyr = xy(randperm(length(xy)));  % shuffle data
    xr = xyr(1:length(x));           % random x-data
    yr = xyr(length(x)+1:end);       % random y-data
    ds(i) = mean(yr) - mean(xr);     % difference of means
end
[h, b] = hist(ds, 20);
h = h/sum(h)/(b(2)-b(1));       % normalization

% significance:
dq = quantile(ds, 0.95);
fprintf('difference of means of null hypothesis at 5%% significance = %.2f\n', dq);
if md >= dq
    fprintf('--> difference of means d=%.2f is significant\n', md);
else
    fprintf('--> d=%.2f is not a significant difference of means\n', md);
end

% plot:
bar(b, h, 'facecolor', 'b');
hold on;
bar(b(b>=dq), h(b>=dq), 'facecolor', 'r');
plot([md md], [0 4], 'r', 'linewidth', 2);
xlabel('Difference of means');
ylabel('Probability density of H0');
hold off;