Be the first to rate this file! 1 Download (last 30 days) File Size: 4.15 KB File ID: #20029

Zero Out Smalls

by Zhigang Xu

 

23 May 2008 (Updated 07 Jul 2008)

To zero out the small elements whose absolute values are smaller than a given criterion.

| Watch this File

File Information
Description

To zero out the small elements in A whose absolute values are smaller than a given criterion, specified by small_cri. By default, small_cri=eps. Due to operations on floating point numbers, many quantities should be zero mathematically but are actually not. If these "fuzzy" quantities are carried over in further calculations, erroneous results may arise. They will also unnecessarily increase the number of non-zero elements in sparse matrices and slow down the calculations.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
06 Jun 2008 Jos x@y.z

This submission lacks a proper H1 line, a calling syntax, a description of the input and output arguments, and an example. The internal comments seem to be adequate.

I will not rate it, as this is quite trivial. The basic one-liner:
  Y = X .* (abs(X)>crit) ;
is fast for both sparse and non-sparse cases, compensating the overhead of the if-statements in this submission.

07 Jun 2008 Zhigang Xu

Hi Jos,

Thanks for your comments, but as the author, I would like to make a response. Please perform the following experiment, which I did with following results:

>>B=rand(5000);
>> A=B;
>> t1=cputime; A=A.*(abs(A)>0.1); t2=cputime-t1; disp(t2)
    1.3281

>> A=B;
>> t1=cputime; if ~issparse(A); id=abs(A)<0.1; A(id)=0; t2=cputime-t1;end; disp(t2)
    0.9375

where the small 0.1 as the small criterion is used just for an illustration. As you can, your suggested code performs 42% slower than mine. When the matrix becomes much bigger, the difference will even gets bigger. It seems that to judge if a matrix is sparse or not does not cause MATLAB any significant time.
  
Thanks for your interest very much however!

Zhigang Xu

11 Jun 2008 Jos x@y.z

You're right on the timing. However, I ran out of memory in your solution pretty soon.

I hope you can fix the other issues I mentioned earlier.

11 Jun 2008 Zhigang Xu

What are the other issues, Jos?

Please login to add a comment or rating.
Updates
07 Jul 2008

The help message is improved.

Tag Activity for this File
Tag Applied By Date/Time
small quantities Zhigang Xu 22 Oct 2008 10:03:01
zeros Zhigang Xu 22 Oct 2008 10:03:01
eps Zhigang Xu 22 Oct 2008 10:03:01

Contact us at files@mathworks.com