19 lines
472 B
Matlab
19 lines
472 B
Matlab
n = 200;
|
|
corrs = [ 1.0, 0.6, 0.0, -0.9 ];
|
|
for k = [1:length(corrs)]
|
|
r = corrs(k);
|
|
x = randn(n, 1);
|
|
y = r*x; % linear dependence of y on x
|
|
% add noise to destroy perfect correlations:
|
|
y = y + sqrt(1.0-r*r)*randn(n, 1);
|
|
% compute correlation coefficient of data:
|
|
rho = corr(x, y);
|
|
subplot(2, 2, k)
|
|
scatter( x, y )
|
|
text( -2, 2.5, sprintf('r=%.1f', rho) )
|
|
xlabel('x')
|
|
ylabel('y')
|
|
xlim([-3.0, 3.0])
|
|
ylim([-3.0, 3.0])
|
|
end
|