Search code examples
matlabsparse-matrix

Sparse multiplication, MATLAB: only certain elements


Is there any built-in or efficient way to calculate only certain elements in a matrix multiplication A*B = C in MATLAB? For example, calculate only the elements of C, (i,j) such that D(i,j) = 1, for some other matrix.


Solution

  • This is one approach:

    [ii, jj] = find(D==1);
    result = sum(A(ii,:).'.*B(:,jj), 1);