-1

I need to delete rows that only have zeros or values near to it, let's say 0 +/- 1e-20. For example:

A=[  7      8
   1e-18    4
     0      0
   1e-19    0]

In this case, the result should be:

A=[7      8
  1e-18   4]

I found the following code, but this only works if there are exact zeros.

A = A(any(A,2),:)

1 Answers1

1

Zero first all those "technically zero" values.

A(A<1e-10)=0; % or any other threshold, depends on your application. 
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • It's possible that the matrix contains negative values. These are also zeroed. So I tried this but it doesn't work: `R(R<1e-10 && R>1e-10)=0` How can I write it right? – Hans Lustiga May 17 '20 at 12:41
  • @HansLustiga read your code: `R<1e-10 && R>1e-10`. You are selecting all values of R that are strictly smaller than `1e-10` but at the same time (AND, &&) they also must be strictly bigger than `1e-10`. No value is **both** smaller and bigger than a constant *at the same time*. Its mathematically impossible. Just check if they are bigger than zero........ – Ander Biguri May 17 '20 at 12:43
  • I forgot the minus sign, it should be called: `R(R<1e-10 && R>-1e-10)=0` If I don't check the second case, e.g. -3 will also turn to zero. – Hans Lustiga May 17 '20 at 12:50
  • Ah, I see. You are not reading the error message, nor googling what it means once read. Not bad advise to do so. `R(R<1e-10 & R>-1e-10)=0` – Ander Biguri May 17 '20 at 13:01
  • Yes, you're right. Now I did research [link](https://stackoverflow.com/questions/16080143/operands-to-the-and-operators-must-be-convertible-to-logical-scalar-values) Thank you for the help. – Hans Lustiga May 17 '20 at 13:11