[linearalg] updated code for octave
This commit is contained in:
parent
0cd4165858
commit
b0decc7df2
@ -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
|
|
@ -35,3 +35,4 @@ xlabel( 'x' );
|
|||||||
ylabel( 'y' );
|
ylabel( 'y' );
|
||||||
|
|
||||||
hold off;
|
hold off;
|
||||||
|
pause;
|
||||||
|
@ -38,11 +38,11 @@ function covareigen( x, y, pm, w, h )
|
|||||||
colormap( 'gray' );
|
colormap( 'gray' );
|
||||||
else
|
else
|
||||||
% scatter plot:
|
% scatter plot:
|
||||||
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( x, y ); %, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
||||||
end
|
end
|
||||||
|
|
||||||
% plot eigenvectors:
|
% 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' );
|
xlabel( 'x' );
|
||||||
ylabel( 'y' );
|
ylabel( 'y' );
|
||||||
@ -76,7 +76,7 @@ function covareigen( x, y, pm, w, h )
|
|||||||
contourf( xp, yp, gauss )
|
contourf( xp, yp, gauss )
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
scatter( nc(:,1), nc(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( nc(:,1), nc(:,2) ); %, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
||||||
end
|
end
|
||||||
xlabel( 'x' );
|
xlabel( 'x' );
|
||||||
ylabel( 'y' );
|
ylabel( 'y' );
|
||||||
|
@ -15,29 +15,29 @@ function covareigen3( x, y, z )
|
|||||||
|
|
||||||
% scatter plot:
|
% scatter plot:
|
||||||
view( 3 );
|
view( 3 );
|
||||||
scatter3( x, y, z, 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
|
scatter3( x, y, z, 0.1, 'b', 'filled' ); %, 'MarkerEdgeColor', 'blue' );
|
||||||
xlabel( 'x' );
|
xlabel( 'x' );
|
||||||
ylabel( 'y' );
|
ylabel( 'y' );
|
||||||
zlabel( 'z' );
|
zlabel( 'z' );
|
||||||
grid on;
|
grid on;
|
||||||
|
|
||||||
% plot eigenvectors:
|
% 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' );
|
%axis( 'equal' );
|
||||||
hold off;
|
hold off;
|
||||||
|
|
||||||
% 2D scatter plots:
|
% 2D scatter plots:
|
||||||
subplot( 2, 6, 4 );
|
subplot( 2, 6, 4 );
|
||||||
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( x, y, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
|
||||||
xlabel( 'x' )
|
xlabel( 'x' )
|
||||||
ylabel( 'y' )
|
ylabel( 'y' )
|
||||||
subplot( 2, 6, 5 );
|
subplot( 2, 6, 5 );
|
||||||
scatter( x, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( x, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
|
||||||
xlabel( 'x' )
|
xlabel( 'x' )
|
||||||
ylabel( 'z' )
|
ylabel( 'z' )
|
||||||
subplot( 2, 6, 6 );
|
subplot( 2, 6, 6 );
|
||||||
scatter( y, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( y, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
|
||||||
xlabel( 'y' )
|
xlabel( 'y' )
|
||||||
ylabel( 'z' )
|
ylabel( 'z' )
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ function covareigen3( x, y, z )
|
|||||||
y = y - mean( y );
|
y = y - mean( y );
|
||||||
% project onto eigenvectors:
|
% project onto eigenvectors:
|
||||||
nx = [ x y z ] * v(:,inx);
|
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' )
|
xlabel( 'ex' )
|
||||||
ylabel( 'ey' )
|
ylabel( 'ey' )
|
||||||
axis( 'equal' );
|
axis( 'equal' );
|
||||||
|
@ -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;
|
n = 10000;
|
||||||
|
|
||||||
% three distributions:
|
% three distributions:
|
||||||
@ -17,3 +14,4 @@ for k = 1:4
|
|||||||
end
|
end
|
||||||
f = figure( 1 );
|
f = figure( 1 );
|
||||||
covareigen3( x, y, z );
|
covareigen3( x, y, z );
|
||||||
|
pause;
|
||||||
|
@ -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:
|
% correlation coefficients:
|
||||||
n = 10000;
|
n = 10000;
|
||||||
x = randn( n, 1 );
|
x = randn( n, 1 );
|
||||||
@ -10,9 +7,9 @@ for r = 0.01:0.19:1
|
|||||||
clf( f );
|
clf( f );
|
||||||
y = r*x + sqrt(1-r^2)*randn( n, 1 );
|
y = r*x + sqrt(1-r^2)*randn( n, 1 );
|
||||||
covareigen( x, y, 0, 5.0, 3.0 );
|
covareigen( x, y, 0, 5.0, 3.0 );
|
||||||
key = waitforbuttonpress;
|
%key = waitforbuttonpress;
|
||||||
|
pause( 1.0 );
|
||||||
end
|
end
|
||||||
return
|
|
||||||
|
|
||||||
% two distributions:
|
% two distributions:
|
||||||
n = 10000;
|
n = 10000;
|
||||||
@ -21,14 +18,12 @@ y1 = randn( n/2, 1 );
|
|||||||
x2 = randn( n/2, 1 );
|
x2 = randn( n/2, 1 );
|
||||||
y2 = randn( n/2, 1 );
|
y2 = randn( n/2, 1 );
|
||||||
f = figure( 1 );
|
f = figure( 1 );
|
||||||
pause( 'on' );
|
|
||||||
for d = 0:1:5
|
for d = 0:1:5
|
||||||
fprintf( 'Distance = %g\n', d );
|
fprintf( 'Distance = %g\n', d );
|
||||||
clf( f );
|
clf( f );
|
||||||
d2 = d / sqrt( 2.0 );
|
d2 = d / sqrt( 2.0 );
|
||||||
x = [ x1; x2 ];
|
x = [ x1; x2 ];
|
||||||
y = [ y1+d2; y2-d2 ];
|
y = [ y1+d2; y2-d2 ];
|
||||||
scrsz = get(0,'ScreenSize');
|
|
||||||
covareigen( x, y, 0, 10.0, 7.0 );
|
covareigen( x, y, 0, 10.0, 7.0 );
|
||||||
%key = waitforbuttonpress;
|
%key = waitforbuttonpress;
|
||||||
pause( 1.0 );
|
pause( 1.0 );
|
||||||
|
@ -41,5 +41,5 @@ function matrixbox( m, s )
|
|||||||
text( 0.8, 0.1, sprintf( '%.3g', m(2,1) ), 'Units', 'normalized' )
|
text( 0.8, 0.1, sprintf( '%.3g', m(2,1) ), 'Units', 'normalized' )
|
||||||
text( 0.9, 0.1, sprintf( '%.3g', m(2,2) ), 'Units', 'normalized' )
|
text( 0.9, 0.1, sprintf( '%.3g', m(2,2) ), 'Units', 'normalized' )
|
||||||
title( s );
|
title( s );
|
||||||
waitforbuttonpress;
|
pause(1.0);
|
||||||
end
|
end
|
||||||
|
@ -39,7 +39,7 @@ function pca2d( x, y, pm, w, h )
|
|||||||
colormap( 'gray' );
|
colormap( 'gray' );
|
||||||
else
|
else
|
||||||
% scatter plot:
|
% scatter plot:
|
||||||
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( x, y, 'b', 'filled' );%, 'MarkerEdgeColor', 'white' );
|
||||||
end
|
end
|
||||||
|
|
||||||
% plot eigenvectors:
|
% plot eigenvectors:
|
||||||
@ -80,7 +80,7 @@ function pca2d( x, y, pm, w, h )
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
% scatter plot:
|
% scatter plot:
|
||||||
scatter( nc(:,1), nc(:,2), 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( nc(:,1), nc(:,2), 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
|
||||||
end
|
end
|
||||||
xlabel( 'n_x' );
|
xlabel( 'n_x' );
|
||||||
ylabel( 'n_y' );
|
ylabel( 'n_y' );
|
||||||
|
@ -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:
|
% correlation coefficients:
|
||||||
n = 10000;
|
n = 10000;
|
||||||
x = randn( n, 1 );
|
x = randn( n, 1 );
|
||||||
@ -27,7 +24,6 @@ for d = 0:1:5
|
|||||||
d2 = d / sqrt( 2.0 );
|
d2 = d / sqrt( 2.0 );
|
||||||
x = [ x1; x2 ];
|
x = [ x1; x2 ];
|
||||||
y = [ y1+d2; y2-d2 ];
|
y = [ y1+d2; y2-d2 ];
|
||||||
scrsz = get(0,'ScreenSize');
|
|
||||||
pca2d( x, y, 0, 10.0, 7.0 );
|
pca2d( x, y, 0, 10.0, 7.0 );
|
||||||
waitforbuttonpress;
|
waitforbuttonpress;
|
||||||
end
|
end
|
||||||
|
@ -15,29 +15,29 @@ function pca3d( x, y, z )
|
|||||||
|
|
||||||
% scatter plot:
|
% scatter plot:
|
||||||
view( 3 );
|
view( 3 );
|
||||||
scatter3( x, y, z, 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
|
scatter3( x, y, z ); % , 0.1, 'b', 'filled', 'MarkerEdgeColor', 'blue' );
|
||||||
xlabel( 'x' );
|
xlabel( 'x' );
|
||||||
ylabel( 'y' );
|
ylabel( 'y' );
|
||||||
zlabel( 'z' );
|
zlabel( 'z' );
|
||||||
grid on;
|
grid on;
|
||||||
|
|
||||||
% plot eigenvectors:
|
% 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' );
|
%axis( 'equal' );
|
||||||
hold off;
|
hold off;
|
||||||
|
|
||||||
% 2D scatter plots:
|
% 2D scatter plots:
|
||||||
subplot( 2, 6, 4 );
|
subplot( 2, 6, 4 );
|
||||||
scatter( x, y, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( x, y, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
|
||||||
xlabel( 'x' )
|
xlabel( 'x' )
|
||||||
ylabel( 'y' )
|
ylabel( 'y' )
|
||||||
subplot( 2, 6, 5 );
|
subplot( 2, 6, 5 );
|
||||||
scatter( x, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( x, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
|
||||||
xlabel( 'x' )
|
xlabel( 'x' )
|
||||||
ylabel( 'z' )
|
ylabel( 'z' )
|
||||||
subplot( 2, 6, 6 );
|
subplot( 2, 6, 6 );
|
||||||
scatter( y, z, 'b', 'filled', 'MarkerEdgeColor', 'white' );
|
scatter( y, z, 'b', 'filled' ); %, 'MarkerEdgeColor', 'white' );
|
||||||
xlabel( 'y' )
|
xlabel( 'y' )
|
||||||
ylabel( 'z' )
|
ylabel( 'z' )
|
||||||
|
|
||||||
@ -51,10 +51,11 @@ function pca3d( x, y, z )
|
|||||||
y = y - mean( y );
|
y = y - mean( y );
|
||||||
% project onto eigenvectors:
|
% project onto eigenvectors:
|
||||||
nx = [ x y z ] * v(:,inx);
|
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' )
|
xlabel( 'ex' )
|
||||||
ylabel( 'ey' )
|
ylabel( 'ey' )
|
||||||
axis( 'equal' );
|
axis( 'equal' );
|
||||||
hold off;
|
hold off;
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -17,3 +17,4 @@ for k = 1:4
|
|||||||
end
|
end
|
||||||
f = figure( 1 );
|
f = figure( 1 );
|
||||||
pca3d( x, y, z );
|
pca3d( x, y, z );
|
||||||
|
pause
|
||||||
|
@ -17,5 +17,5 @@ function simplematrixbox( a, s )
|
|||||||
xlim( [-2 2 ] )
|
xlim( [-2 2 ] )
|
||||||
ylim( [-2 2] )
|
ylim( [-2 2] )
|
||||||
title( s );
|
title( s );
|
||||||
waitforbuttonpress;
|
pause(1.0);
|
||||||
end
|
end
|
||||||
|
@ -5,21 +5,22 @@ load( 'extdata' );
|
|||||||
dt = time( 2) - time(1);
|
dt = time( 2) - time(1);
|
||||||
tinx = round(spiketimes/dt)+1;
|
tinx = round(spiketimes/dt)+1;
|
||||||
|
|
||||||
% plot voltage trace with dettected spikes:
|
% plot voltage trace with detected spikes:
|
||||||
figure( 1 );
|
figure( 1 );
|
||||||
clf;
|
clf;
|
||||||
plot( time, voltage, '-b' )
|
plot( time, voltage, '-b' )
|
||||||
hold on
|
hold on
|
||||||
scatter( time(tinx), voltage(tinx), 'r', 'filled' );
|
scatter( time(tinx), voltage(tinx), 'r', 'filled' );
|
||||||
xlabel( 'time [ms]' );
|
xlabel( 'time [s]' );
|
||||||
ylabel( 'voltage' );
|
ylabel( 'voltage' );
|
||||||
|
xlim([0.1, 0.4])
|
||||||
hold off
|
hold off
|
||||||
|
|
||||||
% spike waveform snippets:
|
% spike waveform snippets:
|
||||||
w = ceil( 0.005/dt );
|
w = ceil( 0.005/dt );
|
||||||
vs = [];
|
vs = zeros(length(tinx), 2*w);
|
||||||
for k=1:length(tinx)
|
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
|
end
|
||||||
ts = time(1:size(vs,2));
|
ts = time(1:size(vs,2));
|
||||||
ts = ts - ts(floor(length(ts)/2));
|
ts = ts - ts(floor(length(ts)/2));
|
||||||
@ -78,9 +79,9 @@ kx = ones( size( nx ) );
|
|||||||
kx(nx<kthresh) = 2;
|
kx(nx<kthresh) = 2;
|
||||||
|
|
||||||
subplot( 4, 1, 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;
|
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;
|
hold off;
|
||||||
xlabel( 'projection onto eigenvector 1' );
|
xlabel( 'projection onto eigenvector 1' );
|
||||||
ylabel( 'projection onto eigenvector 2' );
|
ylabel( 'projection onto eigenvector 2' );
|
||||||
@ -113,6 +114,7 @@ hold off
|
|||||||
% spike trains:
|
% spike trains:
|
||||||
figure( 1 );
|
figure( 1 );
|
||||||
hold on;
|
hold on;
|
||||||
scatter( time(tinx(kinx1)), voltage(tinx(kinx1)), 100.0, 'r', 'filled' );
|
scatter( time(tinx(kinx1)), voltage(tinx(kinx1)), 10.0, 'r', 'filled' );
|
||||||
scatter( time(tinx(kinx2)), voltage(tinx(kinx2)), 100.0, 'g', 'filled' );
|
scatter( time(tinx(kinx2)), voltage(tinx(kinx2)), 10.0, 'g', 'filled' );
|
||||||
hold off;
|
hold off;
|
||||||
|
pause
|
||||||
|
Reference in New Issue
Block a user