annotate scripts/plot/stemleaf.m @ 16005:369b173d6c16

provide stemleaf.m and printd.m
author Michael Godfrey <michaeldgodfrey@gmail.com>
date Tue, 05 Feb 2013 19:56:29 -0500
parents
children 9978c44ab12c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16005
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
1 ## Copyright (C) 2013 Michael D. Godfrey
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
2 ##
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
4 ##
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
6 ## modify it under the terms of the GNU General Public
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
7 ## License as published by the Free Software Foundation;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
8 ## either version 3 of the License, or (at your option) any
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
9 ## later version.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
10 ##
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful,
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
12 ## but WITHOUT ANY WARRANTY; without even the implied
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
13 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
14 ## PURPOSE. See the GNU General Public License for more
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
15 ## details.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
16 ##
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
17 ## You should have received a copy of the GNU General Public
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
18 ## License along with Octave; see the file COPYING. If not,
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
19 ## see <http://www.gnu.org/licenses/>.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
20
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
21
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
22 ## -*- texinfo -*-
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
23 ## @deftypefn {Function File} {} stemleaf (@var{x})
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
24 ## @deftypefnx {Function File} {@var{plot} =} stemleaf (@var{x}, @var{opt})
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
25 ##
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
26 ## Compute and display a stem and leaf plot of the vector @var{x}.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
27 ##
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
28 ## The @var{x} vector is converted to integer by @var{x} = @code{fix} (@var{x}).
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
29 ## If an output argument is provided, the plot is returned as
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
30 ## an array of strings. The first element is the heading
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
31 ## followed by an element for each stem.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
32 ## The default stem step is 10.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
33 ## The @var{x} vector should be integers. It will be treated so that
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
34 ## the last digit is the leaf value and the other digits are
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
35 ## the stems.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
36 ## The leaf digits are not sorted. If sorted leaf values
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
37 ## are wanted, use @code{sort} (@var{x}) before calling @code{stemleaf} (@var{x}).
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
38 ## The stem and leaf plot is described in: Ch. 3,
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
39 ## Exploratory Data Analysis by J. W. Tukey, Addison-Wesley, 1977.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
40 ## @seealso{hist, printd}
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
41 ## @end deftypefn
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
42
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
43 ## Author: Michael D. Godfrey <michaeldgodfrey@gmail.com>
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
44 ## Description: Compute stem and leaf plot
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
45
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
46 function varargout = stemleaf (x, stem_unit)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
47 % Compute and display a stem and leaf plot of the vector x.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
48 % The x vector is converted to integer by x = fix(x).
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
49 % If an output argument is provided, the plot is returned as
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
50 % an array of strings. The first element is the heading
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
51 % followed by an element for each stem.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
52 % The default stem step is 10. If stem_unit is provided
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
53 % the stem step is set to: 10^(stem_unit+1)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
54 % The x vector should be integers. It will be treated so that
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
55 % the last digit is the leaf value and the other digits are
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
56 % the stems.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
57
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
58 % When we first implemented stem and leaf plots in the early
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
59 % 1960's there was some discussion about sorting vs. leaving
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
60 % the leaf entries in the original order in the data. We
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
61 % decided in favor or sorting the leaves for most purposes.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
62 % This is the choice implemented in the SNAP/IEDA system that
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
63 % was written at that time.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
64 % SNAP/IEDA and particularly its stem and leaf plotting were further
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
65 % developed by Hale Trotter, David Hoagland (at Princeton and MIT)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
66 % and others.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
67 % Tukey, in EDA, generally uses unsorted leaves. In addition,
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
68 % he described a wide range of additional display formats. This
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
69 % implementation does not sort the leaves, but if the x vector is
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
70 % sorted then the leaves come out sorted. A simple display
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
71 % format is used.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
72 % I doubt if providing other options is worthwhile. The
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
73 % code can quite easily be modified to provide specific
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
74 % display results. Or, the returned output string can be edited.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
75 % The returned output is an array of strings with each row
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
76 % containing a line of the plot preceded by the lines of header
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
77 % text as the first row. This facilitates annotation.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
78 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
79 % Note that the code has some added complexity due to the need
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
80 % to distinguish both + and - 0 stems.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
81 % The +- stem values are essential for all plots which span 0.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
82 % After dealing with +-0 stems, the added complexity of putting
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
83 % +- data values in the correct stem is minor, but the sign of
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
84 % 0 leaves must be checked. And, the cases where the stems start
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
85 % or end at +- 0 must also be considered.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
86 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
87 % The fact that IEEE floating point defines +- 0 helps make this
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
88 % easier.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
89 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
90 % Michael D. Godfrey January 2013
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
91
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
92 % More could be implemented for better data scaling. And, of course,
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
93 % other options for the kinds of plots described by Tukey could be
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
94 % provided. This may best be left to users.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
95
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
96 if (nargin >= 2)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
97 stem_step = 10^(stem_unit+1);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
98 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
99 stem_step = 10;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
100 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
101 if (any (x == int32 (x)) == 0)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
102 printf ('Input vector truncated to integer values.\n')
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
103 x = fix (x);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
104 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
105 % Avoid use of int32 due to:
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
106 % floor (int32 (-44)/10) == -4 and floor (int32 (-46)/10) = -5 !!!
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
107 % x = sort (fix (x)); % User can decide about sorting x.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
108 % x = fix (x);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
109 % Adjust scale if too small.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
110 % while any(abs((fix(x) - x)) >= abs(x/100))
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
111 % x =10*x;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
112 % endwhile
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
113 % Note that IEEE 754 states that -+ 0 should compare equal.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
114 % This has led to C sort (and therefore Octave) treating them
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
115 % as equal. Thus, sort([ -1 0 -0 1]) yields: -1 0 -0 1.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
116 % and, sort([-1 -0 0 1]) yields: -1 -0 0 1.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
117 % This means that stem-and-leaf plotting cannot rely on sort
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
118 % to order the data as needed for display.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
119
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
120 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
121 if (all((sort(x) == x)) == 1)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
122 hsort = 'sorted.';
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
123 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
124 hsort = 'unsorted.';
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
125 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
126 nx = max (size (x));
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
127 % Determine stem values
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
128 if (min(x) < 0)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
129 if (signbit(max(x)) == 0) % max is positive
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
130 stems = [fix(min(x)/stem_step)-1 : -1 -0];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
131 stems = [stems 0 : fix(max(x)/stem_step)+1 ];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
132 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
133 if (max(x) < 0)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
134 stems = [(fix(min(x)/stem_step)-1) : fix(max(x)/stem_step)];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
135 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
136 stems = [(fix(min(x)/stem_step)-1) : -1 -0];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
137 stems = [stems 0 : fix(max(x)/stem_step)];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
138 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
139 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
140 else % All stems are > 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
141 stems = [fix(min(x)/stem_step) : fix(max(x)/stem_step) + 1];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
142 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
143 %stems
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
144 %x
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
145 nstems = max(size(stems));
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
146 % compute hinges at +- 1.5 * quartiles
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
147 % this requires sorted data!
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
148 xs = sort (x); % Note that sort preserves -0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
149 threeh = 1.5;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
150 two = 2.0;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
151 j = idivide(nx, 4, "fix") + 1; % Use F95 truncation.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
152 k = nx - j + 1;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
153 hl = xs (j);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
154 hu = xs (k);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
155 if ( (nx + 1) == (4 * j) )
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
156 hl = (xs (j + 1) + hl) / two;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
157 hu = (xs (k - 1) + hu) / two;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
158 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
159 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
160 % :::::::: determine h-spread (dh) and fences ::::::::
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
161 dh = hu - hl;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
162 fu = hu + threeh * dh;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
163 fl = hl - threeh * dh;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
164
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
165 % :::::::: find value adjacent to lower fence ::::::::
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
166 for i = 1:j
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
167 if ( xs (i) >= fl ) continue; endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
168 endfor
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
169 ilow = i;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
170 xlo = xs (ilow);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
171 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
172 % :::::::: find value adjacent to upper fence ::::::::
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
173 for i = 1:j
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
174 if ( xs (nx -i + 1) <= fu )continue; endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
175 endfor
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
176 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
177 ihi = nx - i + 1;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
178 xhi = xs (ihi);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
179 %
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
180 % Heading for output:
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
181 plot_out = [''];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
182 plot_out = [plot_out sprintf('stem step: %i, data: %s\nHinges: lo: %g, hi: %g\n',
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
183 stem_step, hsort, xlo, xhi)];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
184 % This may appear to be a good place to use vectorization using the stem and data arrays
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
185 % but the necessary special case treatment of 0 and -0 seems to result in little reduction
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
186 % of complexity, and since this algorithm is for small data vectors only there would be
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
187 % practically no performance improvement.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
188
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
189 % Determine leaves for each stem:
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
190 for kx = 2:nstems
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
191 line_out = [''];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
192 steml = [''];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
193 % Build a string of leaf digits for stem(kx) if stem(kx) <= 0, or stem(kx-1) if stem(kx) > 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
194 % stems -+ 0 have to be handled as special cases.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
195 for xi = 1:nx
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
196 if(signbit(stems(kx)) == 1)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
197 t1 = ((x(xi) <= stems(kx)*10) && (x(xi) > (stems(kx-1)*10)));
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
198 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
199 t1 = ((x(xi) < stems(kx)*10) && (x(xi) >= (stems(kx-1)*10)));
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
200 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
201 % Special tests for stem -+ 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
202 if ((stems(kx) == 0) && signbit(stems(kx)) && (x(xi) == 0)) && !signbit(x(xi))
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
203 t1 = 0;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
204 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
205 if ((stems(kx-1) == 0) && !signbit(stems(kx-1)) && (x(xi) == 0)) && signbit(x(xi))
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
206 t1 = 0;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
207 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
208 % Create line as a string
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
209 if t1
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
210 if (stems(kx) <= 0)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
211 xz = abs (x(xi) - stems(kx)*10);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
212 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
213 xz = abs (x(xi) - stems(kx-1)*10);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
214 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
215 if ((stems(kx) == 0) && signbit(stems(kx)))
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
216 steml = [steml sprintf('%d', abs(x(xi) - stems(kx)*10))];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
217 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
218 steml = [steml sprintf('%d', xz)];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
219 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
220 endif % t1
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
221 endfor % xi = 1:nx
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
222 % Set correct -0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
223 if ((stems(kx) == 0) && signbit(stems(kx)))
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
224 line_out = [line_out sprintf(' -0 | %s', steml)]; % -0 stem.
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
225 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
226 if( stems(kx) < 0)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
227 line_out = [line_out sprintf('%4d | %s', stems(kx), steml)];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
228 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
229 if stems(kx) > 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
230 line_out = [line_out sprintf('%4d | %s', stems(kx-1), steml)];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
231 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
232 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
233 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
234 plot_out = [plot_out; line_out];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
235 endfor % kx = 2:nstems
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
236 if (nargout == 0)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
237 rows = size (plot_out)(1);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
238 cols = size (plot_out)(2);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
239 for k = 1:rows
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
240 printf('%s\n', plot_out(k,1:cols));
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
241 endfor
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
242 else
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
243 varargout{1} = plot_out;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
244 endif
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
245 endfunction
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
246
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
247 %!demo
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
248 %! ## Unsorted plot:
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
249 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
250 %! stemleaf (x, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
251
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
252 %!demo
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
253 %! ## Sorted leaves:
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
254 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
255 %! y = sort(x);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
256 %! stemleaf (y, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
257
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
258 %!demo
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
259 %! ## More data (sorted)
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
260 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 -13 71 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146 21 23 30 10 20 21 30 0 100 110 1 20 0 ];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
261 %! y = sort(x);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
262 %! stemleaf (y, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
263
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
264 %!test
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
265 %! ## test minus to plus
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
266 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44 37 113 124 37 48 127 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 114 126 53 114 96 25 109 7 31 141 46 -13 71 43 117 116 27 7 68 40 31 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 127 31 116 146 21 23 30 10 20 21 30 0 100 110 1 20 0 ];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
267 %! x = sort(x);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
268 %! r2 = ["stem step: 10, data: sorted.\nHinges: lo: 30, hi: 116\n";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
269 %! " -2 | 82";" -1 | 3";" -0 | 2";" 0 | 00177";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
270 %! " 1 | 00112288";" 2 | 001133577777899";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
271 %! " 3 | 000111123456777889";" 4 | 00122233344456788";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
272 %! " 5 | 223788";" 6 | 138";" 7 | 11";" 8 | ";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
273 %! " 9 | 69";" 10 | 04555567999";" 11 | 0133344455566667777899";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
274 %! " 12 | 0011223444555677788";" 13 | 1239";" 14 | 16"];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
275 %! rx = stemleaf (x, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
276 %! assert(r2, rx);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
277 %!test
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
278 %! ## positive values above 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
279 %! x = [22 12 28 52 39 12 11 11 42 38 44 18 44 ];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
280 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: 12, hi: 42\n";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
281 %! " 1 | 22118";" 2 | 28";" 3 | 98";" 4 | 244";" 5 | 2"];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
282 %! rx = stemleaf (x, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
283 %! assert(r2, rx);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
284 %!test
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
285 %! ## negative values below 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
286 %! x = [22 12 28 52 39 12 11 11 42 38 44 18 44];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
287 %! x = -x;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
288 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: -42, hi: -12\n";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
289 %! " -5 | 2";" -4 | 244";" -3 | 98";" -2 | 28";" -1 | 22118"];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
290 %! rx = stemleaf (x, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
291 %! assert(r2, rx);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
292 %!test
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
293 %! ## positive values from 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
294 %! x = [22 12 28 52 39 2 12 0 11 11 42 38 44 18 44];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
295 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: 11, hi: 42\n";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
296 %! " 0 | 20";" 1 | 22118";" 2 | 28";" 3 | 98";" 4 | 244";" 5 | 2"];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
297 %! rx = stemleaf (x, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
298 %! assert(r2, rx);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
299 %!test
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
300 %! ## negative values from 0
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
301 %! x = [22 12 28 52 39 2 12 0 11 11 42 38 44 18 44];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
302 %! x = -x;
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
303 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: -42, hi: -11\n";...
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
304 %! " -5 | 2";" -4 | 244";" -3 | 98";" -2 | 28";" -1 | 22118";" -0 | 20"];
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
305 %! rx = stemleaf (x, 0);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
306 %! assert(r2, rx);
369b173d6c16 provide stemleaf.m and printd.m
Michael Godfrey <michaeldgodfrey@gmail.com>
parents:
diff changeset
307