# Convert near zero complex parts to 0

13 vues (au cours des 30 derniers jours)
Paul Hoffrichter le 1 Avr 2021
In R2020a, I took a complex vector and converted near zero parts to zero. It took two long lines to do it. Can it be done in one line and not so long lines?
yy
yyFix = yy;
yyFix( abs( real(yyFix) ) < 1e-10 ) = 1j * imag(yyFix( abs( real(yyFix) ) < 1e-10 ) );
yyFix( abs( imag(yyFix) ) < 1e-10 ) = real(yyFix( abs( imag(yyFix) ) < 1e-10 ));
yyFix
The good output is:
yy =
1 + 0i
6.12323399573677e-17 + 1i
-1 + 1.22464679914735e-16i
-1.83697019872103e-16 - 1i
yyFix =
1 + 0i
0 + 1i
-1 + 0i
0 - 1i
I see that eps = 2.22044604925031e-16, but I would like to zero out the real and/or imag parts that are not as close to zero as eps.
<<EDIT 2021-04-01>> The +/-1 yy values can have arbitrary amplitudes. I assume that length(yy) > 1e8 for each signal section and/or input file.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Alan Stevens le 1 Avr 2021
Does the following do what you want
yyFix = round(yy*10^n)/10^n;
where you choose n as desired.
##### 6 commentairesAfficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens
Walter Roberson le 2 Avr 2021
mex could, in some cases, work "in place", which would remove the need to create temporary variables for real and imaginary part, then temporary variables for the conditional results, and putting the results together.
Paul Hoffrichter le 2 Avr 2021
Thanks. I will keep this Mex thought in mind and ask another question some time since I don't get how that would work. Another day. :)

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Logical dans Help Center et File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by