From e1c6c32db0157894cc2208f6ef1e46efe72fd8c8 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Mon, 7 Dec 2020 19:53:13 +0100 Subject: [PATCH] [statistics] imporved code style --- header.tex | 2 +- plotstyle.py | 2 +- statistics/code/checkmymedian.m | 12 ++++---- statistics/code/correlations.m | 6 ++-- statistics/code/cumulative.m | 2 +- statistics/code/diehistograms.m | 2 +- statistics/code/gaussianpdf.m | 8 +++--- statistics/code/histogramquartiles.m | 20 ++++++------- statistics/code/quartiles.m | 22 +++++++-------- statistics/code/rollthedie.m | 4 +-- statistics/code/sprintfexamples.m | 42 ++++++++++++++-------------- 11 files changed, 61 insertions(+), 61 deletions(-) diff --git a/header.tex b/header.tex index 29f6ba8..4e98c6b 100644 --- a/header.tex +++ b/header.tex @@ -304,7 +304,7 @@ % \newboolean{showexercisesolutions} \setboolean{showexercisesolutions}{true} -\newcommand{\exercisesolutions}{end} % 0: here, 1: chapter, 2: end +\newcommand{\exercisesolutions}{here} % 0: here, 1: chapter, 2: end % we need this also as numbers: \ifthenelse{\equal{\exercisesolutions}{end}}{\newcommand{\exercisesolutionsnum}{2}}{% \ifthenelse{\equal{\exercisesolutions}{chapter}}{\newcommand{\exercisesolutionsnum}{1}}{% diff --git a/plotstyle.py b/plotstyle.py index 7e3ba8c..cb453f3 100644 --- a/plotstyle.py +++ b/plotstyle.py @@ -43,7 +43,7 @@ filllw = 1.0 fillec = colors['white'] fillalpha = 0.4 filledge = {'linewidth': filllw, 'joinstyle': 'round'} -if int(mpl.__version__.split('.')[0]) < 2: +if mpl_major < 2: del filledge['joinstyle'] # helper lines: diff --git a/statistics/code/checkmymedian.m b/statistics/code/checkmymedian.m index d2d101d..31c7e1a 100644 --- a/statistics/code/checkmymedian.m +++ b/statistics/code/checkmymedian.m @@ -1,11 +1,11 @@ % check whether the median returned by mymedian % really separates a vector into two halfs -for i = 1:140 % loop over different length - for k = 1:10 % try several times - a = randn( i, 1 ); % generate some data - m = mymedian( a ); % compute median - if length( a(a>m) ) ~= length( a(am)) ~= length(a(a=x1)&(x=x1)&(r=q(1)) & (b=q(1)) & (b=q(2)) & (b=q(2)) & (b=q(3)), h(b>=q(3)), 'FaceColor', [0.5 0 0.5] ); +bar(b(b=q(1)) & (b=q(1)) & (b=q(2)) & (b=q(2)) & (b=q(3)), h(b>=q(3)), 'FaceColor', [0.5 0 0.5]); hold off; diff --git a/statistics/code/quartiles.m b/statistics/code/quartiles.m index 8123733..d78a8db 100644 --- a/statistics/code/quartiles.m +++ b/statistics/code/quartiles.m @@ -1,16 +1,16 @@ -function q = quartiles( x ) +function q = quartiles(x) % returns a vector with the first, second, and third quartile % of the vector x - xs = sort( x ); - if ( length( xs ) == 0 ) % no data + xs = sort(x); + if (length(xs) == 0) % no data q = []; - elseif ( rem( length( xs ), 2 ) == 0 ) % even number of data - index = length( xs )/2; - m = (xs( index ) + xs( index+1 ))/2; - q = [ round( xs(length(xs)/4) ), m, xs(round(3*length(xs)/4)) ]; - else % odd number of data - index = (length( xs ) + 1)/2; - m = xs( index ); - q = [ round( xs(length(xs)/4) ), m, xs(round(3*length(xs)/4)) ]; + elseif (rem(length(xs), 2) == 0) % even number of data + index = length(xs)/2; + m = (xs(index) + xs(index+1))/2; + q = [round(xs(length(xs)/4)), m, xs(round(3*length(xs)/4))]; + else % odd number of data + index = (length(xs) + 1)/2; + m = xs(index); + q = [round(xs(length(xs)/4)), m, xs(round(3*length(xs)/4))]; end end diff --git a/statistics/code/rollthedie.m b/statistics/code/rollthedie.m index 1842da8..1eeefc6 100644 --- a/statistics/code/rollthedie.m +++ b/statistics/code/rollthedie.m @@ -1,4 +1,4 @@ -function x = rollthedie( n ) +function x = rollthedie(n) % return a vector with the result of rolling a die n times - x = randi( [1, 6], n, 1 ); + x = randi([1, 6], n, 1); end diff --git a/statistics/code/sprintfexamples.m b/statistics/code/sprintfexamples.m index b8f6b75..b08a5c2 100644 --- a/statistics/code/sprintfexamples.m +++ b/statistics/code/sprintfexamples.m @@ -1,48 +1,48 @@ % sprintf returns a string. % This string can be used to annotate plots using the text() function. -s = sprintf( 'x=%f', pi ) +s = sprintf('x=%f', pi) % fprintf writes directly to console (or into files). % for fprintf you usually want to add the line break '\n': % '%f' formats floating point numbers: -fprintf( 'x=%f\n', pi ) +fprintf('x=%f\n', pi) % The '%f' formatting string can be anywhere in the string: -fprintf( 'x=%fms\n', pi ) +fprintf('x=%fms\n', pi) % There can be arbitrary many '%' formatting strings: -fprintf( 'x=%fms, y=%fkHz\n', pi, 2*pi ) +fprintf('x=%fms, y=%fkHz\n', pi, 2*pi) % The '%' itself is generated by '%%': -fprintf( 'x=%f%%\n', pi ) +fprintf('x=%f%%\n', pi) % A point followed by a number sets the number of digits after the point: -fprintf( 'x=%.2fms\n', pi ) +fprintf('x=%.2fms\n', pi) % The numbers are appropriately rounded: -fprintf( 'x=%.3fms\n', pi ) +fprintf('x=%.3fms\n', pi) % A number right before the point sets the width of the generated output: -fprintf( 'x=%10.3fms\n', pi ) +fprintf('x=%10.3fms\n', pi) % '%e' also formats floating point numbers but forces to write in % exponential style: -fprintf( 'x=%e\n', pi ) +fprintf('x=%e\n', pi) % again, a point and number set the number of digits after the point. -fprintf( 'x=%.1e\n', pi ) +fprintf('x=%.1e\n', pi) % '%g% formats the floating point number to a given number of valid digits % (default is 5): -fprintf( 'x=%g\n', pi ) +fprintf('x=%g\n', pi) % The number of valid digits is not the number of digits after the point: -fprintf( 'x=%.2g\n', pi ) -fprintf( 'x=%.2g\n', 10.123 ) -fprintf( 'x=%.2g\n', 18765.123 ) -fprintf( 'x=%.5g\n', 18765.123 ) +fprintf('x=%.2g\n', pi) +fprintf('x=%.2g\n', 10.123) +fprintf('x=%.2g\n', 18765.123) +fprintf('x=%.5g\n', 18765.123) % '%d' formats integers: -fprintf( 'x=%d\n', 5 ) +fprintf('x=%d\n', 5) % the number defines the width of the output: -fprintf( 'x=%3d\n', 5 ) +fprintf('x=%3d\n', 5) % precedig the width with a '0' fills up the space with leading zeros: -fprintf( 'x=%03d\n', 5 ) +fprintf('x=%03d\n', 5) % '%s' formats a string: -fprintf( 'x=%s\n', 'hallo' ) +fprintf('x=%s\n', 'hallo') % ... aligned to the right: -fprintf( 'x=%10s\n', 'hallo' ) +fprintf('x=%10s\n', 'hallo') % ... unless the width is negative: -fprintf( 'x=%-10s!\n', 'hallo' ) +fprintf('x=%-10s!\n', 'hallo')