Make very small numbers in complex matrix zero

format shortG
a11 = 0;
a12 = exp(-j*0.5*pi);
a21 = exp(-j*0.5*pi);
a22 = 0;
S = [a11, a12; a21, a22]
S =
0 + 0i 6.1232e-17 - 1i 6.1232e-17 - 1i 0 + 0i
A = ((1+a11)*(1-a22)+a12*a21)/(2*a21);
B = ((1+a11)*(1+a22)-a12*a21)/(2*a21);
C = ((1-a11)*(1-a22)-a12*a21)/(2*a21);
D = ((1-a11)*(1+a22)+a12*a21)/(2*a21);
X = [A, B; C, D]
X =
6.1232e-17 - 3.7494e-33i 0 + 1i 0 + 1i 6.1232e-17 - 3.7494e-33i
X_cleanup = X;
X_cleanup(abs(X_cleanup)<0.0001) = 0
X_cleanup =
0 + 0i 0 + 1i 0 + 1i 0 + 0i
Is there a way to automatically ask matlab to output matrix like what's in the X_cleanup instead of X?
Is there anyting under 'format'?

2 comentarios

Mathieu NOE
Mathieu NOE el 5 de Oct. de 2021
hi
why not simply make the clean up a function that you can call anytime ?
Math Works
Math Works el 5 de Oct. de 2021
It's just a bit annoying having to do that.

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 5 de Oct. de 2021
Editada: Matt J el 5 de Oct. de 2021
Is there a way to automatically ask matlab to output matrix like what's in the X_cleanup instead of X?
No, but note that if you expect your input data to be integer-valued, you can avoid the issue by generating them in integer form directly:
format shortG
a11 = 0;
a12 = -1i;
a21 = -1i;
a22 = 0;
S = [a11, a12; a21, a22]
S =
0 + 0i 0 - 1i 0 - 1i 0 + 0i
A = ((1+a11)*(1-a22)+a12*a21)/(2*a21);
B = ((1+a11)*(1+a22)-a12*a21)/(2*a21);
C = ((1-a11)*(1-a22)-a12*a21)/(2*a21);
D = ((1-a11)*(1+a22)+a12*a21)/(2*a21);
X = [A, B; C, D]
X =
0 + 0i 0 + 1i 0 + 1i 0 + 0i

5 comentarios

Math Works
Math Works el 5 de Oct. de 2021
No, they are obviously not integers. They are complex numbers with decimals.
Matt J
Matt J el 5 de Oct. de 2021
Editada: Matt J el 5 de Oct. de 2021
No, they are obviously not integers
No, that is not obvious. In your example, it is clear that the complex numbers are intended to have integer-valued real and imaginary parts. The function call,
a12 = exp(-j*0.5*pi);
would generate a12=-1i in an infinite precision computer.
Math Works
Math Works el 5 de Oct. de 2021
Editada: Math Works el 5 de Oct. de 2021
The formula must work for all inputs and outputs.
I am expecting sensible output matrices like ones in the picture below. I want decimals, but not "1e-32", because that's just zero from an Engineering perspective.
Matt J
Matt J el 5 de Oct. de 2021
Well like I said, you can't make Matlab truncate results to zero on autopilot.
I think you would regret it even if it were implementable. In the situation you describe, Matlab would have no way of knowing if a computation was the final one or just an intermediate step in a future computation you are planning, so basically truncation would occur at every step in your computational pipeline and accumulate unpredictably.
Math Works
Math Works el 6 de Oct. de 2021
Okay, that's a good point.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2021b

Preguntada:

el 5 de Oct. de 2021

Comentada:

el 6 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by