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.
Zhigang Xu (2020). Zero Out Smalls (https://www.mathworks.com/matlabcentral/fileexchange/20029-zero-out-smalls), MATLAB Central File Exchange. Retrieved .
What are the other issues, Jos?
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.
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:
>> t1=cputime; A=A.*(abs(A)>0.1); t2=cputime-t1; disp(t2)
>> t1=cputime; if ~issparse(A); id=abs(A)<0.1; A(id)=0; t2=cputime-t1;end; disp(t2)
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!
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.
The help message is improved.