annotate scripts/plot/stemleaf.m @ 16083:7398b2dd08cb

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