title: Stem and leaf plot algorithm
body: <p>I am trying to implement a stem and plot algorithm in MATLAB for educational purposes. Before I post my code, let me introduce what the steps of my  approach are. Let us consider that we have  two digit numbers:</p>
  
  <pre><code>A=[20 12 13 21 56 13  16 17 22 23 24];
  </code></pre>
  
  <p>Stems can be given by</p>
  
  <pre><code>stems=fix(A/10)
  stems =
       2     1     1     2     5     1     1     1     2     2     2
  </code></pre>
  
  <p>and leafs can be  given by</p>
  
  <pre><code>leaf=fix(mod(A,10))
  
  leaf =
  
       0     2     3     1     6     3     6     7     2     3     4
  </code></pre>
  
  <p>What I have done, is to sort the stems and according to that sort leafs as well:</p>
  
  <pre><code>[stems, index]=sort(stems,'ascend')
  leaf=leaf(index)
  stems =
       1     1     1     1     1     2     2     2     2     2     5
  leaf =
       2     3     3     6     7     0     1     2     3     4     6
  </code></pre>
  
  <p>This is the basic idea:</p>
  
  <ul>
  <li><ol>
  <li>Count the frequency of occurrence of each number in <code>stems</code></li>
  </ol></li>
  <li><ol start="2">
  <li>Take that many elements from <code>leaf</code></li>
  </ol></li>
  </ul>
  
  <p>Repeat this procedure for each stem, where at each step I am shortening the <code>leaf</code> array. So for instance for <code>stems = 1</code>,  we have <code>[5 1]</code>, so I will have</p>
  
  <pre><code>leaf(1:5)
  ans =
       2     3     3     6     7
  leaf(1:5)=[]
  leaf =
       0     1     2     3     4     6
  </code></pre>
  
  <p><code>stems = 2</code> is again 5 times,  so again:</p>
  
  <pre><code>leaf(1:5)
  ans =
       0     1     2     3     4
  leaf(1:5)=[]
  leaf =
       6
  </code></pre>
  
  <p>Now for <code>stems = 5</code>, we have  1 leaf</p>
  
  <pre><code>leaf(1)
  ans =
       6
  </code></pre>
  
  <p>For this I used a map container and I have created the following code:</p>
  
  <pre><code>function stem_leaf_plot(v)
  if ~isnumeric(v)  %  check that  program will accept  array as a  integers
      error( 'Input V must be numeric'); 
  
  end
  stems=fix(v/10);
  leaf=fix(rem(v,10));
  [stems, index]=sort(stems,'ascend');
  leaf=leaf(index);
  string_stems=num2str(stems);
  %%  count  occurence of each stem
  MAP=containers.Map();
  n=length(stems); % total element of  stems array
  for  ii=1:n
      if isKey(MAP,string_stems(ii))
          MAP(string_stems(ii))= MAP(string_stems(ii))+1;
          else
           MAP(string_stems(ii))=1;
      end
  end
  MAP_count=length(MAP);
  
  stem=num2str(cell2mat(keys(MAP)));
  for jj=1:MAP_count
      frequency=(MAP(string_stems(jj)));
      fprintf('leafs of stem %d',stem(jj));
      disp(leaf(1:frequency));
      leaf(1:frequency)=[]; % delete   elements  step by step
  end
  
  end
  </code></pre>
  
  <p>However, the result of my code is</p>
  
  <pre><code>stem_leaf_plot(A)
  leafs of stem 32     2     3     3     6
  leafs of stem 49     7     0     1     2     3     4     6
  </code></pre>
  
  <p>What is wrong?</p>