From: Bruno Luong on
"Matt " <xys(a)whatever.com> wrote in message <heeinc$ha7$1(a)fred.mathworks.com>...
> "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

Could be a nice project indeed, if there is demand out there. I'm not going to involve in coding it though (lack of time).

Bruno
From: Matt on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <heejqv$q09$1(a)fred.mathworks.com>...
>
> Could be a nice project indeed, if there is demand out there. I'm not going to involve in coding it though (lack of time).
>
=======================


Would you be willing to at least test-drive the class I've coded at the bottom of this post (to be put in a file called RobustSparse.m). It will take me some time to fully understand/install your package, whereas my code would be a 1-file installation for you.

If you're willing, the following revised tests should give the correct results. Otherwise, it will default with a warning to the normal sparse indexing behavior

>> A=RobustSparse(100000,100000,1), B=A

B =

(100000,100000) 1

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





%%%%Put in file RobustSparse.m

classdef RobustSparse < double
%RobustSparse - a class essentially the same as sparse @double, but with
%some bug protection in subsref/subsasgn courtesy of Bruno Luong.

methods

function obj=RobustSparse(varargin)
%constructor for RobustSparse

data=sparse(varargin{:});
obj=obj(a)double(data);

end

function objnew=subsref(obj,S)
%SUBSREF for RobustSparse class - use Bruno Luong's routines

nn=length(S.subs);
idx=S.subs{1};

if nn==1 && ~ischar(idx)


try


if islogical(idx)

[i,j]=find(idx);

else%linear index

[i,j]=ind2sub(size(obj),idx);

end

objnew=getsparse(double(obj),i,j);
objnew=reshape(objnew,size(idx));




catch%Default to usual method

disp 'sparse-sub-access not available. Defaulting...'

objnew=subsref(a)double(obj,S);

end

else

objnew=subsref(a)double(obj,S);

end

objnew=RobustSparse(objnew);

end

function objnew=subsasgn(obj,S,rhs)
%SUBSASGN for RobustSparse class - use Bruno Luong's routines

nn=length(S.subs);
idx=S.subs{1};

if nn==1 && ~ischar(idx)


try


if islogical(idx)

[i,j]=find(idx);

else%single linear index

[i,j]=ind2sub(size(obj),idx);

end

objnew=setsparse(double(obj),i,j,rhs);




catch%Default to usual method

disp 'sparse-sub-access not available. Defaulting...'

objnew=subsasgn(a)double(obj,S,rhs);

end

else

objnew=subsasgn(a)double(obj,S,rhs);

end

objnew=RobustSparse(objnew);



end



function obj=uplus(obj)

end

function obj=uminus(obj)

obj=RobustSparse(uminus(a)double(obj));

end

function obj=plus(L,R)

obj = RobustSparse( plus(a)double(L,R) );

end

function obj=minus(L,R)

obj = RobustSparse( minus(a)double(L,R) );

end



function obj=times(L,R)

obj = RobustSparse( times(a)double(L,R) );

end

function obj=mtimes(L,R)

obj = RobustSparse( mtimes(a)double(L,R) );

end

function obj=rdivide(L,R)

obj = RobustSparse( rdivide(a)double(L,R) );

end

function obj=ldivide(L,R)

obj = RobustSparse( ldivide(a)double(L,R) );

end

function obj=mrdivide(L,R)

obj = RobustSparse( mrdivide(a)double(L,R) );

end

function obj=mldivide(L,R)

obj = RobustSparse( mldivide(a)double(L,R) );

end




function obj=inv(L)

obj = RobustSparse( inv(a)double(L) );

end

function obj=sum(L,varargin)

obj = RobustSparse( sum(a)double(L,varargin{:}) );

end


function display(obj)

l=inputname(1);
if isempty(l), l='ans'; end
disp ' '
disp([l ' = ']);
disp ' '
disp(double(obj));

end
end

end
From: Bruno Luong on
"Matt " <xys(a)whatever.com> wrote in message <heeoar$798$1(a)fred.mathworks.com>...

>
> If you're willing, the following revised tests should give the correct results. Otherwise, it will default with a warning to the normal sparse indexing behavior
>

I sure will, but I cannot take a closer look before Wednesday for many reasons. I'm sure you will make the indexing code right Matt as you seems to be very keen on oop.

Thanks,

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

>I'm sure you will make the indexing code right Matt as you seems to be very keen on oop.
====

Well this seems like a particularly good application of it, although hopefully TMW will fix the bug soon and render all this unnecessary.
From: arl on
On 23 Nov, 18:02, "Matt " <x...(a)whatever.com> wrote:
> "Bruno Luong" <b.lu...(a)fogale.findmycountry> wrote in message <heefss$lc....(a)fred.mathworks.com>...
> > %%%%%%%%%%%%%%%%%%
>
> > This is one of the reason why I wrote this package
Thank you for the help!

I also didn't realize that problem...



> >http://www.mathworks.com/matlabcentral/fileexchange/23488-sparse-sub-...
>
> > >> [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)