COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Large Matrix - Replacing NaN with zeros

• Email
• Follow

```Does anyone know a fast way to replace elements equal to NaN with zeros for a very large matrix.  I have tried X = (~isfinite(X)) = 0, but it take forever.

Thanks!
```
 0

See related articles to this posting

```Jon Smith wrote:
> Does anyone know a fast way to replace elements equal to NaN with zeros
> for a very large matrix.  I have tried X = (~isfinite(X)) = 0, but it
> take forever.

X(isnan(X)) = 0;

should be slightly faster (in theory) as it would not have to do the
extra logical operator. For faster than that, you will probably have to
go to a mex routine.
```
 0

```Walter Roberson wrote:
> Jon Smith wrote:
>> Does anyone know a fast way to replace elements equal to NaN with
>> zeros for a very large matrix.  I have tried X = (~isfinite(X)) = 0,
>> but it take forever.
>
> X(isnan(X)) = 0;
>
> should be slightly faster (in theory) as it would not have to do the
> extra logical operator. For faster than that, you will probably have to
> go to a mex routine.

I agree with the isnan suggestion, but I seem to recall that

X(X~=X) = 0;

is even faster.  It exploits the fact that NaN~=NaN returns true.

Give it a try with your matrix and see which is faster.  If it's still
slow you might be running into memory problems by having to create a
temporary logical matrix that is the same size as X.  If that's the case
you might want to do the replacement in blocks (e.g., column-by-column
or row-by-row).

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
```
 0

2 Replies
815 Views

Similar Articles

12/9/2013 12:44:28 AM
page loaded in 5063 ms. (0)

Similar Artilces:

Trailing zeros
I know i'm probably being dim today but....... I would like to dimension a drawing using a tolerance table in the title block as follows: No Decimal Places +/- 1 1 Decimal Place +/- 0.5 2 Decimal Places +/- 0.1 3 Decimal Places +/- 0.01 The thing I need to do is be able to force individual dimensions to have different numbers of trailing zeros How do I do this Thanks, Kev Tools --> Options --> Document options --> Detailing Find "Training zeroes", in the "Dimensioning Standard" block. On Mon, 16 Jun 2008 09:16:43 -0700 (PDT), alphawave <awtltd@gmail.com> wrote: >I know i'm probably being dim today but....... > >I would like to dimension a drawing using a tolerance table in the >title block as follows: > >No Decimal Places +/- 1 >1 Decimal Place +/- 0.5 >2 Decimal Places +/- 0.1 >3 Decimal Places +/- 0.01 > >The thing I need to do is be able to force individual dimensions to >have different numbers of trailing zeros > >How do I do this > >Thanks, > >Kev This sort of "tolerancing" should never have been "invented". -- Cliff I think you

avoid zeros in report
Hi I have a report based on a table with some columns with numbers. Now my problem is that when I display the columns in my report there is a zero a lot of places, and I would rather want to display blanks instead of zero. Can anyone tell me how to do that? Thanks jj On Sat, 11 Sep 2004 18:51:35 +0200, jj wrote: > Hi > > I have a report based on a table with some columns with numbers. > > Now my problem is that when I display the columns in my report there is a > zero a lot of places, and I would rather want to display blanks instead of > zero. Can anyone tell me

Replacing part of matrix with zeros
Hi Everyone, I have a matrix A. Lets say its 5 x 10. Now I wish to perform an operation, where the first 2 columns, say, of A are replaced by all zeros. How is this done? Regards, Ulrik. "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hsdna0\$qbl\$1@fred.mathworks.com>... > Hi Everyone, > > I have a matrix A. Lets say its 5 x 10. > > Now I wish to perform an operation, where the first 2 columns, say, of A are replaced by all zeros. > > How is this done? > > Regards, > > Ulrik. one of the many solutions m=magic(4); m(:,1:2)=0; us Thanks us! Much appreciated. "us " <us@neurol.unizh.ch> wrote in message <hsdnn0\$mff\$1@fred.mathworks.com>... > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hsdna0\$qbl\$1@fred.mathworks.com>... > > Hi Everyone, > > > > I have a matrix A. Lets say its 5 x 10. > > > > Now I wish to perform an operation, where the first 2 columns, say, of A are replaced by all zeros. > > > > How is this done? > > > > Regards, > > > >

fprintf and insignificat zeros
Is there any way to get fprintf to suppress insignificant trailing zeros with fixed-point notation? '%g' will give scientific notation if that is the most compact, but that is not acceptable for my use. E.g. >> sprintf('%g',0.0000023000) ans = 2.3e-006 I need it to output '0.0000023', but I do not know in advance the magnitude of the number, so variants of '%f' doesn't help either. Thanks. oyvist "?yvind " <oyvist@gmail.com> wrote in message <g9qt8q\$7f7 \$1@fred.mathworks.com>... > Is there any way to get fprintf to suppress insignificant > trailing zeros with fixed-point notation? > > '%g' will give scientific notation if that is the most > compact, but that is not acceptable for my use. > > E.g. > >> sprintf('%g',0.0000023000) > > ans = > > 2.3e-006 > > > I need it to output '0.0000023', but I do not know in > advance the magnitude of the number, so variants of '%f' > doesn't help either. How many insignificant trailing zeros are there here: >> sprintf('%.20f\n',0.1) ans