[linearalg] updated code for octave

This commit is contained in:
Jan Benda 2019-01-06 18:30:51 +01:00
parent 0cd4165858
commit b0decc7df2
13 changed files with 35 additions and 93 deletions

View File

@ -1,52 +0,0 @@
% some vectors:
x = [ -1:0.02:1 ];
y = x*0.5 + 0.1*randn( size(x) );
plot( x, y, '.b' );
hold on
plot( x(10), y(10), '.r' );
% new coordinate system:
%e1 = [ 3/5 4/5 ];
%e2 = [ -4/5 3/5 ];
e1 = [ 3 4 ];
e2 = [ -4 3 ];
me1 = sqrt( e1*e1' );
e1 = e1/me1;
me2 = sqrt( e2*e2' );
e2 = e2/me2;
quiver( 0.0, 0.0 , e1(1), e1(2), 1.0, 'r' )
quiver( 0.0, 0.0 , e2(1), e2(2), 1.0, 'g' )
axis( 'equal' )
% project [x y] onto e1 and e2:
% % the long way:
% nx = zeros( size( x ) ); % new x coordinate
% ny = zeros( size( y ) ); % new y coordinates
% for k=1:length(x)
% xvec = [ x(k) y(k) ];
% nx(k) = xvec * e1';
% ny(k) = xvec * e2';
% end
% plot( nx, ny, '.g' );
% the short way:
%nx = [ x' y' ] * e1';
%nx = [x; y]' * e1';
% nx = e1 * [ x; y ];
% ny = e2 * [ x; y ];
% plot( nx, ny, '.g' );
% even shorter:
n = [e1; e2 ] * [ x; y ];
plot( n(1,:), n(2,:), '.g' );
hold off

View File

@ -35,3 +35,4 @@ xlabel( 'x' );
ylabel( 'y' );
hold off;
pause;

View File

@ -38,11 +38,11 @@ function covareigen( x, y, pm, w, h )
colormap( 'gray' );
else
% scatter plot:
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( x, y ); %, 'b', 'filled', 'MarkerEdgeColor', 'white' );
end
% plot eigenvectors:
quiver( ones( 1, 2 ).*mean( x ), ones( 1, 2 )*mean(y), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
quiver( ones( 1, 2 ).*mean( x ), ones( 1, 2 )*mean(y), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', 'r', 'LineWidth', 3 ); %, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
xlabel( 'x' );
ylabel( 'y' );
@ -76,7 +76,7 @@ function covareigen( x, y, pm, w, h )
contourf( xp, yp, gauss )
end
else
scatter( nc(:,1), nc(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( nc(:,1), nc(:,2) ); %, 'b', 'filled', 'MarkerEdgeColor', 'white' );
end
xlabel( 'x' );
ylabel( 'y' );

View File

@ -15,29 +15,29 @@ function covareigen3( x, y, z )
% scatter plot:
view( 3 );
scatter3( x, y, z, 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
scatter3( x, y, z, 0.1, 'b', 'filled' ); %, 'MarkerEdgeColor', 'blue' );
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );
grid on;
% plot eigenvectors:
quiver3( ones( 1, 3 ).*mean( x ), ones( 1, 3 )*mean(y), ones( 1, 3 )*mean(z), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
quiver3( ones( 1, 3 ).*mean( x ), ones( 1, 3 )*mean(y), ones( 1, 3 )*mean(z), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r' ); %, 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
%axis( 'equal' );
hold off;
% 2D scatter plots:
subplot( 2, 6, 4 );
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( x, y, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'y' )
subplot( 2, 6, 5 );
scatter( x, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( x, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'z' )
subplot( 2, 6, 6 );
scatter( y, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( y, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'y' )
ylabel( 'z' )
@ -51,7 +51,7 @@ function covareigen3( x, y, z )
y = y - mean( y );
% project onto eigenvectors:
nx = [ x y z ] * v(:,inx);
scatter( nx(:,1), nx(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( nx(:,1), nx(:,2), 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'ex' )
ylabel( 'ey' )
axis( 'equal' );

View File

@ -1,6 +1,3 @@
scrsz = get( 0, 'ScreenSize' );
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
n = 10000;
% three distributions:
@ -17,3 +14,4 @@ for k = 1:4
end
f = figure( 1 );
covareigen3( x, y, z );
pause;

View File

@ -1,6 +1,3 @@
scrsz = get( 0, 'ScreenSize' );
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
% correlation coefficients:
n = 10000;
x = randn( n, 1 );
@ -10,9 +7,9 @@ for r = 0.01:0.19:1
clf( f );
y = r*x + sqrt(1-r^2)*randn( n, 1 );
covareigen( x, y, 0, 5.0, 3.0 );
key = waitforbuttonpress;
%key = waitforbuttonpress;
pause( 1.0 );
end
return
% two distributions:
n = 10000;
@ -21,14 +18,12 @@ y1 = randn( n/2, 1 );
x2 = randn( n/2, 1 );
y2 = randn( n/2, 1 );
f = figure( 1 );
pause( 'on' );
for d = 0:1:5
fprintf( 'Distance = %g\n', d );
clf( f );
d2 = d / sqrt( 2.0 );
x = [ x1; x2 ];
y = [ y1+d2; y2-d2 ];
scrsz = get(0,'ScreenSize');
covareigen( x, y, 0, 10.0, 7.0 );
%key = waitforbuttonpress;
pause( 1.0 );

View File

@ -41,5 +41,5 @@ function matrixbox( m, s )
text( 0.8, 0.1, sprintf( '%.3g', m(2,1) ), 'Units', 'normalized' )
text( 0.9, 0.1, sprintf( '%.3g', m(2,2) ), 'Units', 'normalized' )
title( s );
waitforbuttonpress;
pause(1.0);
end

View File

@ -39,7 +39,7 @@ function pca2d( x, y, pm, w, h )
colormap( 'gray' );
else
% scatter plot:
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( x, y, 'b', 'filled' );%, 'MarkerEdgeColor', 'white' );
end
% plot eigenvectors:
@ -80,7 +80,7 @@ function pca2d( x, y, pm, w, h )
end
else
% scatter plot:
scatter( nc(:,1), nc(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( nc(:,1), nc(:,2), 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
end
xlabel( 'n_x' );
ylabel( 'n_y' );

View File

@ -1,6 +1,3 @@
scrsz = get( 0, 'ScreenSize' );
set( 0, 'DefaultFigurePosition', [ scrsz(3)/2 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2 ] );
% correlation coefficients:
n = 10000;
x = randn( n, 1 );
@ -27,7 +24,6 @@ for d = 0:1:5
d2 = d / sqrt( 2.0 );
x = [ x1; x2 ];
y = [ y1+d2; y2-d2 ];
scrsz = get(0,'ScreenSize');
pca2d( x, y, 0, 10.0, 7.0 );
waitforbuttonpress;
end

View File

@ -15,29 +15,29 @@ function pca3d( x, y, z )
% scatter plot:
view( 3 );
scatter3( x, y, z, 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
scatter3( x, y, z ); % , 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );
grid on;
% plot eigenvectors:
quiver3( ones( 1, 3 ).*mean( x ), ones( 1, 3 )*mean(y), ones( 1, 3 )*mean(z), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r', 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
quiver3( ones( 1, 3 ).*mean( x ), ones( 1, 3 )*mean(y), ones( 1, 3 )*mean(z), v(1,:).*sqrt(diag(d))', v(2,:).*sqrt(diag(d))', v(3,:).*sqrt(diag(d))', 'r' ); %, 'LineWidth', 3, 'AutoScale', 'off', 'AutoScaleFactor', 1.0, 'MaxHeadSize', 0.7 )
%axis( 'equal' );
hold off;
% 2D scatter plots:
subplot( 2, 6, 4 );
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( x, y, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'y' )
subplot( 2, 6, 5 );
scatter( x, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( x, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'x' )
ylabel( 'z' )
subplot( 2, 6, 6 );
scatter( y, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( y, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'y' )
ylabel( 'z' )
@ -51,10 +51,11 @@ function pca3d( x, y, z )
y = y - mean( y );
% project onto eigenvectors:
nx = [ x y z ] * v(:,inx);
scatter( nx(:,1), nx(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
scatter( nx(:,1), nx(:,2), 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
xlabel( 'ex' )
ylabel( 'ey' )
axis( 'equal' );
hold off;
end

View File

@ -17,3 +17,4 @@ for k = 1:4
end
f = figure( 1 );
pca3d( x, y, z );
pause

View File

@ -17,5 +17,5 @@ function simplematrixbox( a, s )
xlim( [-2 2 ] )
ylim( [-2 2] )
title( s );
waitforbuttonpress;
pause(1.0);
end

View File

@ -5,21 +5,22 @@ load( 'extdata' );
dt = time( 2) - time(1);
tinx = round(spiketimes/dt)+1;
% plot voltage trace with dettected spikes:
% plot voltage trace with detected spikes:
figure( 1 );
clf;
plot( time, voltage, '-b' )
hold on
scatter( time(tinx), voltage(tinx), 'r', 'filled' );
xlabel( 'time [ms]' );
xlabel( 'time [s]' );
ylabel( 'voltage' );
xlim([0.1, 0.4])
hold off
% spike waveform snippets:
w = ceil( 0.005/dt );
vs = [];
vs = zeros(length(tinx), 2*w);
for k=1:length(tinx)
vs = [ vs; voltage(tinx(k)-w:tinx(k)+w-1) ];
vs(k,:) = voltage(tinx(k)-w:tinx(k)+w-1);
end
ts = time(1:size(vs,2));
ts = ts - ts(floor(length(ts)/2));
@ -78,9 +79,9 @@ kx = ones( size( nx ) );
kx(nx<kthresh) = 2;
subplot( 4, 1, 2 );
scatter( nx(kx==1), ny(kx==1), 'r', 'filled', 'MarkerEdgeColor', 'white' );
scatter( nx(kx==1), ny(kx==1), 10.0, 'r', 'filled' ); %, 'MarkerEdgeColor', 'white' );
hold on;
scatter( nx(kx==2), ny(kx==2), 'g', 'filled', 'MarkerEdgeColor', 'white' );
scatter( nx(kx==2), ny(kx==2), 10.0, 'g', 'filled' ); %, 'MarkerEdgeColor', 'white' );
hold off;
xlabel( 'projection onto eigenvector 1' );
ylabel( 'projection onto eigenvector 2' );
@ -113,6 +114,7 @@ hold off
% spike trains:
figure( 1 );
hold on;
scatter( time(tinx(kinx1)), voltage(tinx(kinx1)), 100.0, 'r', 'filled' );
scatter( time(tinx(kinx2)), voltage(tinx(kinx2)), 100.0, 'g', 'filled' );
scatter( time(tinx(kinx1)), voltage(tinx(kinx1)), 10.0, 'r', 'filled' );
scatter( time(tinx(kinx2)), voltage(tinx(kinx2)), 10.0, 'g', 'filled' );
hold off;
pause