From: Chad on
Greetings,

I used to have this line of code:

D(~isfinite(D)) = 0;

However, as the size of D has grown, I've had to go to sparse D. As a result, isfinite(D) returns an out-of-memory error. I could do something like:

D=sparse(N,M);

% lots-of-stuff

for a=1:N*M;
if ~isfinite(D(a))
D(a)=0;
end
end

But then I get the error "FOR loop index is too large. Truncating to 2147483647. "

Any clever suggestions of built in commands I'm unaware of?

Thanks
From: Matt J on
"Chad " <parishcm(a)ornl.gov> wrote in message <ho887g$kmr$1(a)fred.mathworks.com>...
> Greetings,
>
> I used to have this line of code:
>
> D(~isfinite(D)) = 0;
>
> However, as the size of D has grown, I've had to go to sparse D. As a result, isfinite(D) returns an out-of-memory error. I could do something like:
===========

You could try something like this

idx=spfun(@(x) ~isfinite(x), D);
D(idx)=0;

Beware, however, that linear and logical indexing of large sparse matrices are vulnerable to certain bugs (as of R2009b). You might therefore want to use my RobustSparse data types instead

http://www.mathworks.com/matlabcentral/fileexchange/26181-robust-sparse-data-types

or else use Bruno's tools on which this is based,

http://www.mathworks.com/matlabcentral/fileexchange/23488-sparse-sub-access
From: James Tursa on
"Chad " <parishcm(a)ornl.gov> wrote in message <ho887g$kmr$1(a)fred.mathworks.com>...
> Greetings,
>
> I used to have this line of code:
>
> D(~isfinite(D)) = 0;
>
> However, as the size of D has grown, I've had to go to sparse D. As a result, isfinite(D) returns an out-of-memory error. I could do something like:
>
> D=sparse(N,M);
>
> % lots-of-stuff
>
> for a=1:N*M;
> if ~isfinite(D(a))
> D(a)=0;
> end
> end
>
> But then I get the error "FOR loop index is too large. Truncating to 2147483647. "
>
> Any clever suggestions of built in commands I'm unaware of?
>
> Thanks

It would be fairly easy (and execute very fast) to implement this in a C mex routine. I can probably whip something up today and post it. Is your D always real or is it possibly complex?

James Tursa
From: Chad on
Matt J,

Thank you for the suggestions, I will look into their possible implementations.

James Tursa,

Thank you for the offer! I hate to impose upon someones else's time, but if you're willing to do so, I'd be happy for your help. Real numbers, always.
From: Bruno Luong on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <ho8aq0$3cv$1(a)fred.mathworks.com>...
> "Chad " <parishcm(a)ornl.gov> wrote in message <ho887g$kmr$1(a)fred.mathworks.com>...
> > Greetings,
> >
> > I used to have this line of code:
> >
> > D(~isfinite(D)) = 0;
> >
> > However, as the size of D has grown, I've had to go to sparse D. As a result, isfinite(D) returns an out-of-memory error. I could do something like:
> ===========
>
> You could try something like this
>
> idx=spfun(@(x) ~isfinite(x), D);
> D(idx)=0;
>
> Beware, however, that linear and logical indexing of large sparse matrices are vulnerable to certain bugs (as of R2009b). You might therefore want to use my RobustSparse data types instead
>

Indeed, the bug is still not fixed in 2010A:

>> D=sparse(1e5,1e5,Inf)

D =

(100000,100000) Inf

>> D(isinf(D))=0

D =

(100000,100000) Inf

>> D=rsparse(D) % Matt's robust sparse

D=

(100000,100000) Inf

>> D(isinf(D))=0 % OK

D=

All zero sparse: 100000-by-100000

>> D=sparse(D) % convert to Matlab sparse if wanted

D =

All zero sparse: 100000-by-100000

% Bruno