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.