From: Michael Snyder on
Hi Y'all,

I am working with a large 4D matrix using many iterations to solve a problem.

My problem is once in a while my matrix operations produce a non-number, inf, -inf, or even an nan. There is no easy way to know ahead of time when this happens.

I am looking for the fastest possible way to convert the non-numbers to zeros between iterations. If I convert the non-numbers to zeros between iterations then it does not effect my final results.

I have gotten my main iteration loop down to 1.5 seconds. If I use the following code to convert non-numbers to zeros, it adds 2.7 seconds to my loop. This isn’t good. I have tried converting my 4D matrix to 3D matrixes, but it doesn’t speed up my code in this case

maxtrix_out(maxtrix_out==Inf)=0;
maxtrix_out(maxtrix_out==-Inf)=0;
maxtrix_out(maxtrix_out==NaN)=0;

There has to be a faster way. Can anyone point me in the right direction?

Thanks,

Michael
From: John D'Errico on
"Michael Snyder" <sirzerp(a)mathworks.com> wrote in message <huiulq$75v$1(a)fred.mathworks.com>...
> Hi Y'all,
>
> I am working with a large 4D matrix using many iterations to solve a problem.
>
> My problem is once in a while my matrix operations produce a non-number, inf, -inf, or even an nan. There is no easy way to know ahead of time when this happens.
>
> I am looking for the fastest possible way to convert the non-numbers to zeros between iterations. If I convert the non-numbers to zeros between iterations then it does not effect my final results.
>
> I have gotten my main iteration loop down to 1.5 seconds. If I use the following code to convert non-numbers to zeros, it adds 2.7 seconds to my loop. This isn&#8217;t good. I have tried converting my 4D matrix to 3D matrixes, but it doesn&#8217;t speed up my code in this case
>
> maxtrix_out(maxtrix_out==Inf)=0;
> maxtrix_out(maxtrix_out==-Inf)=0;
> maxtrix_out(maxtrix_out==NaN)=0;
>
> There has to be a faster way. Can anyone point me in the right direction?

Well, actually, you are wasting some time here.
Try this test...

>> nan == nan
ans =
0

Instead, read these.

help isnan
help isinf

matrix(isnan(matrix) | isinf(matrix)) = 0;

John
From: Michael Snyder on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <huivka$e2u$1(a)fred.mathworks.com>...
> "Michael Snyder" <sirzerp(a)mathworks.com> wrote in message <huiulq$75v$1(a)fred.mathworks.com>...
> > Hi Y'all,
> >
> > I am working with a large 4D matrix using many iterations to solve a problem.
> >
> > My problem is once in a while my matrix operations produce a non-number, inf, -inf, or even an nan. There is no easy way to know ahead of time when this happens.
> >
> > I am looking for the fastest possible way to convert the non-numbers to zeros between iterations. If I convert the non-numbers to zeros between iterations then it does not effect my final results.
> >
> > I have gotten my main iteration loop down to 1.5 seconds. If I use the following code to convert non-numbers to zeros, it adds 2.7 seconds to my loop. This isn&#8217;t good. I have tried converting my 4D matrix to 3D matrixes, but it doesn&#8217;t speed up my code in this case
> >
> > maxtrix_out(maxtrix_out==Inf)=0;
> > maxtrix_out(maxtrix_out==-Inf)=0;
> > maxtrix_out(maxtrix_out==NaN)=0;
> >
> > There has to be a faster way. Can anyone point me in the right direction?
>
> Well, actually, you are wasting some time here.
> Try this test...
>
> >> nan == nan
> ans =
> 0
>
> Instead, read these.
>
> help isnan
> help isinf
>
> matrix(isnan(matrix) | isinf(matrix)) = 0;
>
> John

Thanks John

Maybe 2.7 seconds wasn't that bad. :)

46.640 1 x 43 maxtrix_out(isnan(maxtrix_out) | isinf(maxtrix_out)) = 0

I try your command with 3d matrixes, but for sure it doesn't like 4d, at 46 seconds. :)
From: Walter Roberson on
Michael Snyder wrote:

> I am working with a large 4D matrix using many iterations to solve a
> problem.

> My problem is once in a while my matrix operations produce a non-number,
> inf, -inf, or even an nan. There is no easy way to know ahead of time
> when this happens.
>
> I am looking for the fastest possible way to convert the non-numbers to
> zeros between iterations. If I convert the non-numbers to zeros between
> iterations then it does not effect my final results.

The most direct way (though I do not promise it is the fastest
available) would be

matrix_out(~isfinite(matrix_out)) = 0;
From: Walter Roberson on
Michael Snyder wrote:

> Maybe 2.7 seconds wasn't that bad. :)
>
> 46.640 1 x 43 maxtrix_out(isnan(maxtrix_out) |
> isinf(maxtrix_out)) = 0
> I try your command with 3d matrixes, but for sure it doesn't like 4d, at
> 46 seconds. :)

Those command should be very nearly insensitive to number of dimensions,
as this is just straight-forward logical indexing, which internally just
copies the matrix dimensions and then sequences through consequative
memory locations.
 |  Next  |  Last
Pages: 1 2
Prev: Help in patch command ...
Next: Fault signal