0

In Octave, one can do an element-wise multiplication between a full matrix and compatible (broadcastable) vector (i.e. MxN .* 1xN or MxN .* Mx1). But this does not seem to be applicable for sparse matrix.

Consider the following example,

v = (1:5)';
s = spdiags(v,0,5,5);   %  simple sparse matrix

s .* v;        % <--- error 'nonconformant arguments (op1 is 5x5, op2 is 5x1)'
full(s) .* v;  % <--- works but defeats sparse matrix

In the above simple case, with a diagonal sparse matrix, converting to full matrix can be avoided by converting v to diagonal matrix i.e.

s * diag(v);  % <--- returns desired result
diag(v) * s;  % <--- also results desired result

but for other cases, i.e. non-diagonal sparse matrix, it gets unnecessarily complicated by operand-order.

Is there a trick to doing broadcastable operation with sparse matrix? ...else is this a bug or feature (i.e. necessary)?

codechimp
  • 1,509
  • 1
  • 14
  • 21
  • 1
    broadcasting simply does not work with sparse matrices. See octave [bug #41441](https://savannah.gnu.org/bugs/?41441) – carandraug Nov 25 '19 at 18:51
  • I haven't benchmarked, but perhaps the sensible approach here is to perform matrix multiplication and use it to create a sparse matrix again?, e.g. `spdiags( s * v, 0, 5, 5 )` ... ( but I understand you're just making a simple testcase here ) – Tasos Papastylianou Nov 26 '19 at 11:52

0 Answers0