title: randomly select histogram data MATLAB
body: <p>I have an input 2D histogram that I want to do 2-fold cross-validation with. The problem is I don't know how to extract two mutually exclusive random samples of the data from a histogram. If it was a couple of lists of the positional information of each data point, that would be easy - shuffle the data in the lists in the same way, and split the lists equally.</p>
  
  <p>So for a list I would do this:</p>
  
  <pre><code>list1 = [1,2,3,3,5,6,1];
  list2 = [1,3,6,6,5,2,1];
  
  idx = randperm(length(list1)); % ie. idx = [4 3 1 5 6 2 7]
  shlist1 = list1(idx); % shlist1 = [3,3,1,5,6,2,1]
  shlist2 = list2(idx); % shlist2 = [6,6,1,5,2,3,1]
  
  slist1 = shlist1(1:3); % slist1 = [3,3,1]
  elist1 = shlist1(4:6); % elist1 = [5,6,2,1]
  slist2 = shlist2(1:3); % slist2 = [6,6,1]
  elist2 = shlist2(4:6); % elist2 = [5,2,3,1]
  </code></pre>
  
  <p>But if this same data was presented to me as a histogram</p>
  
  <pre><code>hist = [2 0 0 0 0 0]
         [0 0 0 0 0 1]
         [0 1 0 0 0 0]
         [0 0 0 0 0 0]
         [0 0 0 0 1 0]
         [0 0 2 0 0 0]
  </code></pre>
  
  <p>I want the result to be something like this</p>
  
  <pre><code>hist1 = [0 0 0 0 0 0]
          [0 0 0 0 0 1]
          [0 1 0 0 0 0]
          [0 0 0 0 0 0]
          [0 0 0 0 0 0]
          [0 0 1 0 0 0]
  
  hist2 = [2 0 0 0 0 0]
          [0 0 0 0 0 0]
          [0 0 0 0 0 0]
          [0 0 0 0 0 0]
          [0 0 0 0 1 0]
          [0 0 1 0 0 0]
  </code></pre>
  
  <p>so that different halves of the data are randomly, and equally assigned to two new histograms. </p>
  
  <p>Would this be equivalent to taking a random integer height of each bin hist(i,j), and adding that to the equivalent bin in hist1(i,j), and the difference to hist2(i,j)?</p>
  
  <pre><code>% hist as shown above
  hist1 = zeros(6);
  hist2 = zeros(6);
  for i = 1:length(hist(:,1))*length(hist(1,:))
      randNum = rand;
      hist1(i) = round(hist(i)*randNum);
      hist2(i) = hist(i) - hist1(i);
  end
  </code></pre>
  
  <p>And if that is equivalent, is there a better way/built-in way of doing it?</p>
  
  <p>My actual histogram is 300x300 bins, and contains about 6,000,000 data points, and it needs to be fast.</p>
  
  <p>Thanks for any help :)</p>
  
  <p>EDIT:
  The suggested bit of code I made is not equivalent to taking a random sample of positional points from a list, as it does not maintain the overall probability density function of the data.
  Halving the histograms should be fine for my 6,000,000 points, but I was hoping for a method that would still work for few points.</p>