comparison scripts/plot/stemleaf.m @ 16087:2b994ee38b1c

maint: backout accidental check-in 979ebfdd240d.
author Rik <rik@octave.org>
date Thu, 21 Feb 2013 22:31:13 -0800
parents 979ebfdd240d
children 3e6d15a2a50b
comparison
equal deleted inserted replaced
16086:f89760972bf4 16087:2b994ee38b1c
19 ## see <http://www.gnu.org/licenses/>. 19 ## see <http://www.gnu.org/licenses/>.
20 20
21 21
22 ## -*- texinfo -*- 22 ## -*- texinfo -*-
23 ## @deftypefn {Function File} {} stemleaf (@var{x}) 23 ## @deftypefn {Function File} {} stemleaf (@var{x})
24 ## @deftypefnx {Function File} {} stemleaf (@var{x}, @var{stem_sz}) 24 ## @deftypefnx {Function File} {@var{plot} =} stemleaf (@var{x}, @var{opt})
25 ## @deftypefnx {Function File} {@var{plotstr} =} stemleaf (@dots{}) 25 ##
26 ## Compute and display a stem and leaf plot of the vector @var{x}. 26 ## Compute and display a stem and leaf plot of the vector @var{x}.
27 ## 27 ##
28 ## The input @var{x} should be a vector of integers. Any non-integer values 28 ## The @var{x} vector is converted to integer by @var{x} = @code{fix} (@var{x}).
29 ## will be converted to integer by @code{@var{x} = fix (@var{x})}. Each 29 ## If an output argument is provided, the plot is returned as
30 ## element of @var{x} will be plotted with the last digit of the element as a 30 ## an array of strings. The first element is the heading
31 ## leaf value and the remaining digits as the stem. For example, 123 will be 31 ## followed by an element for each stem.
32 ## plotted with the stem @samp{12} and the leaf @samp{3}. 32 ## The default stem step is 10.
33 ## 33 ## The @var{x} vector should be integers. It will be treated so that
34 ## The optional input @var{stem_sz} sets the width of each stem. 34 ## the last digit is the leaf value and the other digits are
35 ## The stem width is determined by @code{10^(@var{stem_sz} + 1)}. 35 ## the stems.
36 ## The default stem width is 10. 36 ## The leaf digits are not sorted. If sorted leaf values
37 ## 37 ## are wanted, use @code{sort} (@var{x}) before calling @code{stemleaf} (@var{x}).
38 ## With no return argument, the plot is immediately displayed. If an output 38 ## The stem and leaf plot is described in: Ch. 3,
39 ## argument is provided, the plot is returned as an array of strings. 39 ## Exploratory Data Analysis by J. W. Tukey, Addison-Wesley, 1977.
40 ##
41 ## The leaf digits are not sorted. If sorted leaf values are desired, use
42 ## @code{@var{xs} = sort (@var{x})} before calling @code{stemleaf (@var{xs})}.
43 ##
44 ## The stem and leaf plot is described in: Ch. 3, @cite{Exploratory
45 ## Data Analysis} by J. W. Tukey, Addison-Wesley, 1977.
46 ## @seealso{hist, printd} 40 ## @seealso{hist, printd}
47 ## @end deftypefn 41 ## @end deftypefn
48 42
49 ## Author: Michael D. Godfrey <michaeldgodfrey@gmail.com> 43 ## Author: Michael D. Godfrey <michaeldgodfrey@gmail.com>
50 ## Description: Compute stem and leaf plot 44 ## Description: Compute stem and leaf plot
51 45
52 function plotstr = stemleaf (x, stem_sz) 46 function varargout = stemleaf (x, stem_unit)
53 ## Compute and display a stem and leaf plot of the vector x. The x 47 ## Compute and display a stem and leaf plot of the vector x. The x
54 ## vector is converted to integer by x = fix(x). If an output argument 48 ## vector is converted to integer by x = fix(x). If an output argument
55 ## is provided, the plot is returned as an array of strings. The 49 ## is provided, the plot is returned as an array of strings. The
56 ## first element is the heading followed by an element for each stem. 50 ## first element is the heading followed by an element for each stem.
57 ## 51 ##
58 ## The default stem step is 10. If stem_sz is provided the stem 52 ## The default stem step is 10. If stem_unit is provided the stem
59 ## step is set to: 10^(stem_sz+1). The x vector should be integers. 53 ## step is set to: 10^(stem_unit+1) The x vector should be integers.
60 ## It will be treated so that the last digit is the leaf value and the 54 ## It will be treated so that the last digit is the leaf value and the
61 ## other digits are the stems. 55 ## other digits are the stems.
62 ## 56 ##
63 ## When we first implemented stem and leaf plots in the early 1960's 57 ## When we first implemented stem and leaf plots in the early 1960's
64 ## there was some discussion about sorting vs. leaving the leaf 58 ## there was some discussion about sorting vs. leaving the leaf
65 ## entries in the original order in the data. We decided in favor of 59 ## entries in the original order in the data. We decided in favor or
66 ## sorting the leaves for most purposes. This is the choice 60 ## sorting the leaves for most purposes. This is the choice
67 ## implemented in the SNAP/IEDA system that was written at that time. 61 ## implemented in the SNAP/IEDA system that was written at that time.
68 ## 62 ##
69 ## SNAP/IEDA, and particularly its stem and leaf plotting, were further 63 ## SNAP/IEDA and particularly its stem and leaf plotting were further
70 ## developed by Hale Trotter, David Hoagland (at Princeton and MIT), 64 ## developed by Hale Trotter, David Hoagland (at Princeton and MIT)
71 ## and others. 65 ## and others.
72 ## 66 ##
73 ## Tukey, in EDA, generally uses unsorted leaves. In addition, he 67 ## Tukey, in EDA, generally uses unsorted leaves. In addition, he
74 ## described a wide range of additional display formats. This 68 ## described a wide range of additional display formats. This
75 ## implementation does not sort the leaves, but if the x vector is 69 ## implementation does not sort the leaves, but if the x vector is
76 ## sorted then the leaves come out sorted. A simple display format is 70 ## sorted then the leaves come out sorted. A simple display format is
77 ## used. 71 ## used.
78 ## 72 ##
79 ## I doubt if providing other options is worthwhile. The code can 73 ## I doubt if providing other options is worthwhile. The code can
80 ## quite easily be modified to provide specific display results. Or, 74 ## quite easily be modified to provide specific display results. Or,
81 ## the returned output string can be edited. The returned output is an 75 ## the returned output string can be edited. The returned output is an
82 ## array of strings with each row containing a line of the plot 76 ## array of strings with each row containing a line of the plot
83 ## preceded by the lines of header text as the first row. This 77 ## preceded by the lines of header text as the first row. This
84 ## facilitates annotation. 78 ## facilitates annotation.
85 ## 79 ##
86 ## Note that the code has some added complexity due to the need to 80 ## Note that the code has some added complexity due to the need to
87 ## distinguish both + and - 0 stems. The +- stem values are essential 81 ## distinguish both + and - 0 stems. The +- stem values are essential
88 ## for all plots which span 0. After dealing with +-0 stems, the added 82 ## for all plots which span 0. After dealing with +-0 stems, the added
89 ## complexity of putting +- data values in the correct stem is minor, 83 ## complexity of putting +- data values in the correct stem is minor,
90 ## but the sign of 0 leaves must be checked. And, the cases where the 84 ## but the sign of 0 leaves must be checked. And, the cases where the
91 ## stems start or end at +- 0 must also be considered. 85 ## stems start or end at +- 0 must also be considered.
92 ## 86 ##
93 ## The fact that IEEE floating point defines +- 0 helps make this 87 ## The fact that IEEE floating point defines +- 0 helps make this
94 ## easier. 88 ## easier.
95 ## 89 ##
90 ##
96 ## Michael D. Godfrey January 2013 91 ## Michael D. Godfrey January 2013
97 92
98 ## More could be implemented for better data scaling. And, of course, 93 ## More could be implemented for better data scaling. And, of course,
99 ## other options for the kinds of plots described by Tukey could be 94 ## other options for the kinds of plots described by Tukey could be
100 ## provided. This may best be left to users. 95 ## provided. This may best be left to users.
101 96
102 if (nargin < 1 || nargin > 2) 97 if (nargin >= 2)
103 print_usage (); 98 stem_step = 10^(stem_unit+1);
104 endif 99 else
105 100 stem_step = 10;
106 if (! isvector (x)) 101 endif
107 error ("stemleaf: X must be a vector"); 102 if (any (x == int32 (x)) == 0)
108 endif 103 printf ('Input vector truncated to integer values.\n')
109 104 x = fix (x);
110 if (isinteger (x)) 105 endif
111 ## Avoid use of int32 because rounding rules do not use fix(): 106
112 ## floor (int32 (-44)/10) == -4 and floor (int32 (-46)/10) = -5 !!! 107 ## Avoid use of int32 due to:
113 x = single (x); 108
114 elseif (isfloat (x)) 109 ## floor (int32 (-44)/10) == -4 and floor (int32 (-46)/10) = -5 !!!
115 xint = fix (x); 110 ## x = sort (fix (x)); % User can decide about sorting x.
116 if (any (x != xint)) 111 ## x = fix (x);
117 warning ("stemleaf: X truncated to integer values"); 112 ## %Adjust scale if too small.
118 x = xint; 113 ## while any(abs((fix(x) - x)) >= abs(x/100))
114 ## x =10*x;
115 ## endwhile
116
117 ## Note that IEEE 754 states that -+ 0 should compare equal. This has
118 ## led to C sort (and therefore Octave) treating them as equal. Thus,
119 ## sort([ -1 0 -0 1]) yields: -1 0 -0 1. and, sort([-1 -0 0 1])
120 ## yields: -1 -0 0 1. This means that stem-and-leaf plotting cannot
121 ## rely on sort to order the data as needed for display.
122
123 if (all((sort(x) == x)) == 1)
124 hsort = 'sorted.';
125 else
126 hsort = 'unsorted.';
127 endif
128 nx = max (size (x));
129 ## Determine stem values
130 if (min(x) < 0)
131 if (signbit(max(x)) == 0) # max is positive
132 stems = [fix(min(x)/stem_step)-1 : -1 -0];
133 stems = [stems 0 : fix(max(x)/stem_step)+1 ];
134 else
135 if (max(x) < 0)
136 stems = [(fix(min(x)/stem_step)-1) : fix(max(x)/stem_step)];
137 else
138 stems = [(fix(min(x)/stem_step)-1) : -1 -0];
139 stems = [stems 0 : fix(max(x)/stem_step)];
140 endif
119 endif 141 endif
120 else 142 else # All stems are > 0
121 error ("stemleaf: X must be a numeric vector"); 143 stems = [fix(min(x)/stem_step) : fix(max(x)/stem_step) + 1];
122 endif 144 endif
123 145 ##stems
124 if (nargin == 1) 146 ##x
125 stem_step = 10; 147 nstems = max(size(stems));
126 else
127 if (isscalar (stem_sz) && stem_sz >= 0 && isreal (stem_sz))
128 stem_step = 10^(stem_sz+1);
129 else
130 error ("stemleaf: STEM_SZ must be a real integer >= 0");
131 endif
132 endif
133
134 ## Note that IEEE 754 states that -+ 0 should compare equal. This has
135 ## led to C sort (and therefore Octave) treating them as equal. Thus,
136 ## sort([-1 0 -0 1]) yields [-1 0 -0 1], and sort([-1 -0 0 1])
137 ## yields: [-1 -0 0 1]. This means that stem-and-leaf plotting cannot
138 ## rely on sort to order the data as needed for display.
139 ## This also applies to min()/max() so these routines can't be relied
140 ## upon if the max or min is -+ 0.
141
142 if (issorted (x))
143 hsort = "sorted.";
144 else
145 hsort = "unsorted.";
146 endif
147
148 ## Determine stem values
149 min_x = min (x);
150 max_x = max (x);
151 if (min_x > 0) # all stems > 0
152 stems = [fix(min(x)/stem_step) : (fix(max(x)/stem_step)+1)];
153 elseif (max_x < 0) # all stems < 0
154 stems = [(fix(min_x/stem_step)-1) : fix(max_x/stem_step)];
155 elseif (min_x < 0 && max_x > 0) # range crosses 0
156 stems = [(fix(min_x/stem_step)-1) : -1 , -0];
157 stems = [stems, 0 : fix(max_x/stem_step)+1 ];
158 else # range includes a zero which may be +0 or -0
159 if (min_x == 0)
160 if (any (x == 0 & signbit (x)))
161 min_x = -0;
162 else
163 min_x = +0;
164 endif
165 endif
166 if (max_x == 0)
167 if (any (x == 0 & ! signbit (x)))
168 max_x = +0;
169 else
170 max_x = -0;
171 endif
172 endif
173 stems = [];
174 if (signbit (min_x))
175 stems = [(fix(min_x/stem_step)-1) : -1 , -0];
176 endif
177 if (! signbit (max_x))
178 stems = [stems, 0 : fix(max_x/stem_step)+1 ];
179 endif
180 endif
181
182 nx = numel (x);
183 nstems = numel (stems);
184 ## compute hinges at +- 1.5 * quartiles 148 ## compute hinges at +- 1.5 * quartiles
185 ## this requires sorted data! 149 ## this requires sorted data!
186 xs = sort (x); # Note that sort preserves -0 150 xs = sort (x); # Note that sort preserves -0
187 j = fix (nx/4) + 1; 151 threeh = 1.5;
152 two = 2.0;
153 j = idivide(nx, 4, "fix") + 1; # Use F95 truncation.
188 k = nx - j + 1; 154 k = nx - j + 1;
189 hl = xs(j); 155 hl = xs (j);
190 hu = xs(k); 156 hu = xs (k);
191 ## FIXME: Is this really the only case where we want to average 157 if ( (nx + 1) == (4 * j) )
192 ## values to determine the quartile? 158 hl = (xs (j + 1) + hl) / two;
193 if ( (nx + 1) == (4 * j) ) 159 hu = (xs (k - 1) + hu) / two;
194 hl = (xs(j + 1) + hl) / 2; 160 endif
195 hu = (xs(k - 1) + hu) / 2; 161
196 endif 162 ## :::::::: determine h-spread (dh) and fences ::::::::
197
198 ## determine h-spread (dh) and fences
199 dh = hu - hl; 163 dh = hu - hl;
200 fu = hu + 1.5 * dh; 164 fu = hu + threeh * dh;
201 fl = hl - 1.5 * dh; 165 fl = hl - threeh * dh;
202 166
203 ## find value adjacent to lower fence 167 ## :::::::: find value adjacent to lower fence ::::::::
204 for i = 1:j 168 for i = 1:j
205 if (xs(i) >= fl) 169 if ( xs (i) >= fl )
206 break; 170 continue;
207 endif 171 endif
208 endfor 172 endfor
209 xlo = xs(i); 173 ilow = i;
210 174 xlo = xs (ilow);
211 ## find value adjacent to upper fence 175
212 for i = 1:j 176 ## :::::::: find value adjacent to upper fence ::::::::
213 if (xs(nx -i + 1) <= fu) 177 for i = 1:j
214 break; 178 if ( xs (nx -i + 1) <= fu )
179 continue;
215 endif 180 endif
216 endfor 181 endfor
217 xhi = xs(nx - i + 1); 182
218 183 ihi = nx - i + 1;
219 ## Summary at start of output: 184 xhi = xs (ihi);
220 plot_out = sprintf ("stem step: %i, data: %s", stem_step, hsort); 185
221 plot_out = [plot_out; sprintf("Hinges: lo: %g, hi: %g", xlo, xhi)]; 186 ## Heading for output:
222 plot_out = [plot_out; " "]; 187 plot_out = "";
188 plot_out = [plot_out sprintf("stem step: %i, data: %s\nHinges: lo: %g, hi: %g\n",
189 stem_step, hsort, xlo, xhi)];
223 190
224 ## This may appear to be a good place to use vectorization using the 191 ## This may appear to be a good place to use vectorization using the
225 ## stem and data arrays but the necessary special case treatment of 0 192 ## stem and data arrays but the necessary special case treatment of 0
226 ## and -0 seems to result in little reduction of complexity, and since 193 ## and -0 seems to result in little reduction of complexity, and since
227 ## this algorithm is for small data vectors only there would be 194 ## this algorithm is for small data vectors only there would be
228 ## practically no performance improvement. 195 ## practically no performance improvement.
229 196
230 ## Determine leaves for each stem: 197 ## Determine leaves for each stem:
231 for kx = 2:nstems 198 for kx = 2:nstems
232 line = ""; 199 line_out = "";
233 steml = ""; 200 steml = "";
234 ## Build a string of leaf digits for 201 ## Build a string of leaf digits for stem(kx) if stem(kx) <= 0, or
235 ## stem(kx) if stem(kx) <= 0, or
236 ## stem(kx-1) if stem(kx) > 0 202 ## stem(kx-1) if stem(kx) > 0
237 203
238 ## stems -+ 0 have to be handled as special cases. 204 ## stems -+ 0 have to be handled as special cases.
239 for xi = 1:nx 205 for xi = 1:nx
240 if (signbit (stems(kx)) != 0) 206 if(signbit(stems(kx)) != 0)
241 t1 = ((x(xi) <= stems(kx)*10) && (x(xi) > (stems(kx-1)*10))); 207 t1 = ((x(xi) <= stems(kx)*10) && (x(xi) > (stems(kx-1)*10)));
242 else 208 else
243 t1 = ((x(xi) < stems(kx)*10) && (x(xi) >= (stems(kx-1)*10))); 209 t1 = ((x(xi) < stems(kx)*10) && (x(xi) >= (stems(kx-1)*10)));
244 endif 210 endif
245 ## Special tests for stem -+ 0 211 ## Special tests for stem -+ 0
246 if ((stems(kx) == 0) 212 if ((stems(kx) == 0) && signbit(stems(kx)) && (x(xi) == 0)) && !signbit(x(xi))
247 && signbit (stems(kx)) && (x(xi) == 0)) && !signbit (x(xi))
248 t1 = 0; 213 t1 = 0;
249 endif 214 endif
250 if ((stems(kx-1) == 0) 215 if ((stems(kx-1) == 0) && !signbit(stems(kx-1)) && (x(xi) == 0)) && signbit(x(xi))
251 && !signbit (stems(kx-1)) && (x(xi) == 0)) && signbit (x(xi))
252 t1 = 0; 216 t1 = 0;
253 endif 217 endif
254 ## Create line as a string 218 ## Create line as a string
255 if (t1) 219 if t1
256 if (stems(kx) <= 0) 220 if (stems(kx) <= 0)
257 xz = abs (x(xi) - stems(kx)*10); 221 xz = abs (x(xi) - stems(kx)*10);
258 else 222 else
259 xz = abs (x(xi) - stems(kx-1)*10); 223 xz = abs (x(xi) - stems(kx-1)*10);
260 endif 224 endif
261 if ((stems(kx) == 0) && signbit (stems(kx))) 225 if ((stems(kx) == 0) && signbit(stems(kx)))
262 steml = [steml sprintf("%d", abs(x(xi) - stems(kx)*10))]; 226 steml = [steml sprintf("%d", abs(x(xi) - stems(kx)*10))];
263 else 227 else
264 steml = [steml sprintf("%d", xz)]; 228 steml = [steml sprintf("%d", xz)];
265 endif 229 endif
266 endif # t1 230 endif % t1
267 endfor # xi = 1:nx 231 endfor % xi = 1:nx
268 232
269 ## Set correct -0 233 ## Set correct -0
270 if ((stems(kx) == 0) && signbit (stems(kx))) 234 if ((stems(kx) == 0) && signbit(stems(kx)))
271 line = [line sprintf(" -0 | %s", steml)]; # -0 stem. 235 line_out = [line_out sprintf(" -0 | %s", steml)]; % -0 stem.
272 else 236 else
273 if (stems(kx) < 0) 237 if( stems(kx) < 0)
274 line = [line sprintf("%4d | %s", stems(kx), steml)]; 238 line_out = [line_out sprintf("%4d | %s", stems(kx), steml)];
275 else 239 else
276 if (stems(kx) > 0) 240 if stems(kx) > 0
277 line = [line sprintf("%4d | %s", stems(kx-1), steml)]; 241 line_out = [line_out sprintf("%4d | %s", stems(kx-1), steml)];
278 endif 242 endif
279 endif 243 endif
280 endif 244 endif
281 plot_out = [plot_out; line]; 245 plot_out = [plot_out; line_out];
282 endfor # kx = 2:nstems 246 endfor % kx = 2:nstems
283
284 if (nargout == 0) 247 if (nargout == 0)
285 disp (plot_out); 248 rows = size (plot_out)(1);
249 cols = size (plot_out)(2);
250 for k = 1:rows
251 printf("%s\n", plot_out(k,1:cols));
252 endfor
286 else 253 else
287 plotstr = plot_out; 254 varargout{1} = plot_out;
288 endif 255 endif
289
290 endfunction 256 endfunction
291
292 257
293 %!demo 258 %!demo
294 %! ## Unsorted plot: 259 %! ## Unsorted plot:
295 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44]; 260 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44];
296 %! stemleaf (x); 261 %! stemleaf (x, 0);
297 262
298 %!demo 263 %!demo
299 %! ## Sorted leaves: 264 %! ## Sorted leaves:
300 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44]; 265 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44];
301 %! y = sort (x); 266 %! y = sort(x);
302 %! stemleaf (y); 267 %! stemleaf (y, 0);
303 268
304 %!demo 269 %!demo
305 %! ## Sorted leaves (large dataset): 270 %! ## More data (sorted)
306 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44 37 113 124 37 48 127 \ 271 %! 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 ];
307 %! 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 \ 272 %! y = sort(x);
308 %! 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 \ 273 %! stemleaf (y, 0);
309 %! 114 126 53 114 96 25 109 7 31 141 46 -13 71 43 117 116 27 7 68 40 31 \
310 %! 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57\
311 %! 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 \
312 %! 127 31 116 146 21 23 30 10 20 21 30 0 100 110 1 20 0];
313 %! y = sort (x);
314 %! stemleaf (y);
315
316 %!demo
317 %! ## Gaussian leaves:
318 %! x = fix (30 * randn (300,1));
319 %! stemleaf (x);
320 274
321 %!test 275 %!test
322 %! ## test minus to plus 276 %! ## test minus to plus
323 %! x = [-22 12 -28 52 39 -2 12 10 11 11 42 38 44 18 44 37 113 124 37 48 127 \ 277 %! 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 ];
324 %! 36 29 31 125 139 131 115 105 132 104 123 35 113 122 42 117 119 58 109 \ 278 %! x = sort(x);
325 %! 23 105 63 27 44 105 99 41 128 121 116 125 32 61 37 127 29 113 121 58 \ 279 %! r2 = ["stem step: 10, data: sorted.\nHinges: lo: 30, hi: 116\n";...
326 %! 114 126 53 114 96 25 109 7 31 141 46 -13 71 43 117 116 27 7 68 40 31 \ 280 %! " -2 | 82";" -1 | 3";" -0 | 2";" 0 | 00177";...
327 %! 115 124 42 128 52 71 118 117 38 27 106 33 117 116 111 40 119 47 105 57\ 281 %! " 1 | 00112288";" 2 | 001133577777899";...
328 %! 122 109 124 115 43 120 43 27 27 18 28 48 125 107 114 34 133 45 120 30 \ 282 %! " 3 | 000111123456777889";" 4 | 00122233344456788";...
329 %! 127 31 116 146 21 23 30 10 20 21 30 0 100 110 1 20 0]; 283 %! " 5 | 223788";" 6 | 138";" 7 | 11";" 8 | ";...
330 %! x = sort (x); 284 %! " 9 | 69";" 10 | 04555567999";" 11 | 0133344455566667777899";...
331 %! rexp = char ( 285 %! " 12 | 0011223444555677788";" 13 | 1239";" 14 | 16"];
332 %! "stem step: 10, data: sorted." , 286 %! rx = stemleaf (x, 0);
333 %! "Hinges: lo: -28, hi: 146" , 287 %! assert(r2, rx);
334 %! " " ,
335 %! " -2 | 82" ,
336 %! " -1 | 3" ,
337 %! " -0 | 2" ,
338 %! " 0 | 00177" ,
339 %! " 1 | 00112288" ,
340 %! " 2 | 001133577777899" ,
341 %! " 3 | 000111123456777889" ,
342 %! " 4 | 00122233344456788" ,
343 %! " 5 | 223788" ,
344 %! " 6 | 138" ,
345 %! " 7 | 11" ,
346 %! " 8 | " ,
347 %! " 9 | 69" ,
348 %! " 10 | 04555567999" ,
349 %! " 11 | 0133344455566667777899",
350 %! " 12 | 0011223444555677788" ,
351 %! " 13 | 1239" ,
352 %! " 14 | 16" );
353 %! r = stemleaf (x, 0);
354 %! assert (r, rexp);
355
356 %!test 288 %!test
357 %! ## positive values above 0 289 %! ## positive values above 0
358 %! x = [22 12 28 52 39 12 11 11 42 38 44 18 44]; 290 %! x = [22 12 28 52 39 12 11 11 42 38 44 18 44 ];
359 %! rexp = char ( 291 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: 12, hi: 42\n";...
360 %! "stem step: 10, data: unsorted." , 292 %! " 1 | 22118";" 2 | 28";" 3 | 98";" 4 | 244";" 5 | 2"];
361 %! "Hinges: lo: 11, hi: 52" , 293 %! rx = stemleaf (x, 0);
362 %! " " , 294 %! assert(r2, rx);
363 %! " 1 | 22118" ,
364 %! " 2 | 28" ,
365 %! " 3 | 98" ,
366 %! " 4 | 244" ,
367 %! " 5 | 2" );
368 %! r = stemleaf (x);
369 %! assert (r, rexp);
370
371 %!test 295 %!test
372 %! ## negative values below 0 296 %! ## negative values below 0
373 %! x = [22 12 28 52 39 12 11 11 42 38 44 18 44]; 297 %! x = [22 12 28 52 39 12 11 11 42 38 44 18 44];
374 %! x = -x; 298 %! x = -x;
375 %! rexp = char ( 299 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: -42, hi: -12\n";...
376 %! "stem step: 10, data: unsorted." , 300 %! " -5 | 2";" -4 | 244";" -3 | 98";" -2 | 28";" -1 | 22118"];
377 %! "Hinges: lo: -52, hi: -11" , 301 %! rx = stemleaf (x, 0);
378 %! " " , 302 %! assert(r2, rx);
379 %! " -5 | 2" ,
380 %! " -4 | 244" ,
381 %! " -3 | 98" ,
382 %! " -2 | 28" ,
383 %! " -1 | 22118" );
384 %! r = stemleaf (x);
385 %! assert (r, rexp);
386
387 %!test 303 %!test
388 %! ## positive values from 0 304 %! ## positive values from 0
389 %! x = [22 12 28 52 39 2 12 0 11 11 42 38 44 18 44]; 305 %! x = [22 12 28 52 39 2 12 0 11 11 42 38 44 18 44];
390 %! rexp = char ( 306 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: 11, hi: 42\n";...
391 %! "stem step: 10, data: unsorted." , 307 %! " 0 | 20";" 1 | 22118";" 2 | 28";" 3 | 98";" 4 | 244";" 5 | 2"];
392 %! "Hinges: lo: 0, hi: 52" , 308 %! rx = stemleaf (x, 0);
393 %! " " , 309 %! assert(r2, rx);
394 %! " 0 | 20" ,
395 %! " 1 | 22118" ,
396 %! " 2 | 28" ,
397 %! " 3 | 98" ,
398 %! " 4 | 244" ,
399 %! " 5 | 2" );
400 %! r = stemleaf (x);
401 %! assert (r, rexp);
402
403 %!test 310 %!test
404 %! ## negative values from 0 311 %! ## negative values from 0
405 %! x = [22 12 28 52 39 2 12 0 11 11 42 38 44 18 44]; 312 %! x = [22 12 28 52 39 2 12 0 11 11 42 38 44 18 44];
406 %! x = -x; 313 %! x = -x;
407 %! rexp = char ( 314 %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: -42, hi: -11\n";...
408 %! "stem step: 10, data: unsorted." , 315 %! " -5 | 2";" -4 | 244";" -3 | 98";" -2 | 28";" -1 | 22118";" -0 | 20"];
409 %! "Hinges: lo: -52, hi: -0" , 316 %! rx = stemleaf (x, 0);
410 %! " " , 317 %! assert(r2, rx);
411 %! " -5 | 2" , 318
412 %! " -4 | 244" ,
413 %! " -3 | 98" ,
414 %! " -2 | 28" ,
415 %! " -1 | 22118" ,
416 %! " -0 | 20" );
417 %! r = stemleaf (x);
418 %! assert (r, rexp);
419
420 %!test
421 %! ## both +0 and -0 present
422 %! x = [-9 -7 -0 0];
423 %! rexp = char (
424 %! "stem step: 10, data: sorted." ,
425 %! "Hinges: lo: -9, hi: 0" ,
426 %! " " ,
427 %! " -0 | 970" ,
428 %! " 0 | 0" );
429 %! r = stemleaf (x);
430 %! assert (r, rexp);
431
432 ## Test input validation
433 %!error stemleaf ()
434 %!error stemleaf (1, 2, 3)
435 %!error <X must be a vector> stemleaf (ones (2,2))
436 %!warning <X truncated to integer values> tmp = stemleaf ([0 0.5 1]);
437 %!error <X must be a numeric vector> stemleaf ("Hello World")
438 %!error <STEM_SZ must be a real integer> stemleaf (1, ones (2,2))
439 %!error <STEM_SZ must be a real integer> stemleaf (1, -1)
440 %!error <STEM_SZ must be a real integer> stemleaf (1, 1+i)
441