title: "dimension too large" error when broadcasting to sparse matrix in octave
body: <p>32-bit Octave has a limit on the maximum number of elements in an array.  I have recompiled from source (following the script at <a href="https://github.com/calaba/octave-3.8.2-enable-64-ubuntu-14.04" rel="nofollow">https://github.com/calaba/octave-3.8.2-enable-64-ubuntu-14.04</a> ), and now have 64-bit indexing.</p>
  
  <p>Nevertheless, when I attempt to perform elementwise multiplication using a broadcast function, I get <code>error: out of memory or dimension too large for Octave's index type</code></p>
  
  <p>Is this a bug, or an undocumented feature?  If it's a bug, does anyone have a reasonably efficient workaround?</p>
  
  <p>Minimal code to reproduce the problem:</p>
  
  <pre><code>function indexerror();
  % both of these are formed without error
  %    a = zeros (2^32, 1, 'int8');
  %    b = zeros (1024*1024*1024*3, 1, 'int8');
  
  %   sizemax    % returns 9223372036854775806
  
      nnz = 1000            % number of non-zero elements
      rowmax = 250000
      colmax = 100000
  
      irow = zeros(1,nnz);
      icol = zeros(1,nnz);
      for ind =1:nnz
          irow(ind) = round(rowmax/nnz*ind);
          icol(ind) = round(colmax/nnz*ind);
      end
  
      sparseMat = sparse(irow,icol,1,rowmax,colmax);
  
      % column vector to be broadcast
      broad = 1:rowmax;
      broad = broad(:);
  
      % this gives "dimension too large" error
      toobig = bsxfun(@times,sparseMat,broad);
  
      % so does this
      toobig2 = sparse(repmat(broad,1,size(sparseMat,2)));
      mult = sparse( sparseMat .* toobig2 );        % never made it this far
  end
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Well, I have an <em>inefficient</em> workaround.  It's slower than using <code>bsxfun</code> by a factor of 3 or so (depending on the details), but it's better than having to sort through the error in the libraries. Hope someone finds this useful some day.</p>
  
  <pre><code>% loop over rows, instead of using bsxfun
  mult_loop = sparse([],[],[],rowmax,colmax);
  for ind =1:length(broad);
      mult_loop(ind,:) = broad(ind) * sparseMat(ind,:);
  end
  </code></pre>