title: 2 columns in VAL field of accumarray - Matlab
body: <p>I am not very comfortable with using accumarray function in Matlab, though I have begun to appreciate its powers!  I was wondering if I could input 2 cols in the VAL field of accumarray function.  Please see -</p>
  
  <pre><code>sz = 3  ; % num_rows for each ID
  mat1 = [1 20 ; 1 40 ; 1 50 ; 2 10 ; 2 100 ; 2 110] ; % Col1 is ID, Col2 is Value
  idx  = [30 1000 ; 30 1200 ; 30 1500 ; 30 1000 ; 30 1200 ; 30 1500 ] ; 
  % col1: index ID, col2: value
  </code></pre>
  
  <p>mat1 is ID returns while idx is index returns.  For simplicity, idx returns are repeated to match mat1.  All IDs in mat1 have same rows.  Even idx has the same rows.</p>
  
  <pre><code>[~,~,n] = unique(mat1(:,1), 'rows', 'last') ;
  fncovariance = @(x,y) (x.*y)/sz ;
  accumarray(n, [x(:,2) y(:,2)], [], fncovariance) % --&gt; FAILS as VAL is not-vector!
  </code></pre>
  
  <p>You can see that I'm trying to calculate covariance (cov(x,y,1)) but cannot use Matlab's function directly as mat1 has IDs and I need covariance for each ID w.r.t Index.  </p>
  
  <p>Ansmat:</p>
  
  <pre><code>    1 2444.4
      2 7888.9
  </code></pre>