From: arl on
Hello, I was wondering if anyone knows a solution to divide element-
wise two sparse matrices, A./B, but considering only the positions
where A is non-zero.

I though in using spfun, but it works only with one variable input.

Thank you in advance.
From: Anon on
Hi Arl,

what about this?

C = A.*spfun(@(x) 1./x,B);

Best regards, anon

arl <arlourenco(a)gmail.com> wrote in message <f495641d-048f-423d-a618-676f969fa814(a)p36g2000vbn.googlegroups.com>...
> Hello, I was wondering if anyone knows a solution to divide element-
> wise two sparse matrices, A./B, but considering only the positions
> where A is non-zero.
>
> I though in using spfun, but it works only with one variable input.
>
> Thank you in advance.
From: Matt on
arl <arlourenco(a)gmail.com> wrote in message <f495641d-048f-423d-a618-676f969fa814(a)p36g2000vbn.googlegroups.com>...
> Hello, I was wondering if anyone knows a solution to divide element-
> wise two sparse matrices, A./B, but considering only the positions
> where A is non-zero.
>
> I though in using spfun, but it works only with one variable input.
>
> Thank you in advance.

result=A;
idx=logical(A);
result(idx)=A(idx)./B(idx);
From: Bruno Luong on
"Matt " <xys(a)whatever.com> wrote in message <heedra$cuq$1(a)fred.mathworks.com>...

>
> result=A;
> idx=logical(A);
> result(idx)=A(idx)./B(idx);

CAREFUL!!!! Matlab is known for being BUGGY in doing such indexing with sparse

result(idx)=A(idx)./B(idx);

>> A=sparse(100000,100000,1)

A =

(100000,100000) 1

>> B=A

B =

(100000,100000) 1

>> result=A;
>> idx=logical(A);
>> result(idx)=A(idx)./B(idx)

result =

(65408,14101) 1
(100000,100000) 1

%%%%%%%%%%%%%%%%%%

This is one of the reason why I wrote this package
http://www.mathworks.com/matlabcentral/fileexchange/23488-sparse-sub-access

>> [i j]=find(A);
>> AB = setsparse(A,i,j,getsparse(B,i,j),@(a,b) a./b)

AB =

(100000,100000) 1

% Bruno
From: Matt on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <heefss$lcl$1(a)fred.mathworks.com>...

> %%%%%%%%%%%%%%%%%%
>
> This is one of the reason why I wrote this package
> http://www.mathworks.com/matlabcentral/fileexchange/23488-sparse-sub-access
>
> >> [i j]=find(A);
> >> AB = setsparse(A,i,j,getsparse(B,i,j),@(a,b) a./b)
>
> AB =
>
> (100000,100000) 1
>
> % Bruno


I didn't realize the extent of the problem. It wonder if one could provide a subclass of @double to interface to this package. That way, we could use the old indexing syntax instead of things like this

AB = setsparse(A,i,j,getsparse(B,i,j),@(a,b) a./b)