79197964

Date: 2024-11-17 19:24:45
Score: 0.5
Natty:
Report link

A solution that might generalise nicely (and I find easier to read) is to use a binary mask to pick out the non-zeros elements (as a sparse logical matrix), e.g. mask=(A~=0)

I've illustrated how to use it in this handy function that would do what you want. With your example, you could call this as A=spfun2(f,A,B);

function Y=spfun2(fun,A,B)

mask=(A~=0); % a sparse logical, so still quite efficient?

if nnz(B(mask))~=nnz(mask)
    warning('Non-zeros of B are not fully aligned with non-zeros of A')
end

Y=spalloc(size(A,1),size(A,2),nnz(A)); 
%or simply Y=A; has the same effect of making Y the right size

Y(mask)=fun(A(mask),B(mask));

Thanks, I was banging up against a related issue and this was the only place I found useful discussion of it.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Trevor Agus