File Exchange

## Zero Out Smalls

version 1.0.0.0 (4.15 KB) by
To zero out the small elements whose absolute values are smaller than a given criterion.

Updated 07 Jul 2008

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.

### Cite As

Zhigang Xu (2020). Zero Out Smalls (https://www.mathworks.com/matlabcentral/fileexchange/20029-zero-out-smalls), MATLAB Central File Exchange. Retrieved .

Zhigang Xu

What are the other issues, Jos?

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.

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

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.