rounding issues in matlab, need to force values to 0

4 visualizaciones (últimos 30 días)
bob
bob el 2 de Feb. de 2022
Respondida: Benjamin Thompson el 2 de Feb. de 2022
How do force matlab to set specfic function values at 0 instead of 1.34e-18 or something small. the if and else statements dont work and the treshold code (X(X<treshold))=0; doesnt work as that defaults some of my Xn (y1.....y4) values to be null/0 and phase becomes 0.
%y2 = 100e-3*1/5*sinc(n*1/5);
%y3 = 100e-3*1/7*sinc(n*1/7);
%y4 = 100e-3*1/2*( sinc (n*1/2) ).^2;
n = -15:1:15;
y1 = 100e-3*1/2*sinc(n*1/2);
if abs(y1)< 1e-6
y1=0;
end
figure(1)
stem(n,abs(y1),'r','LineWidth',1.4)
ylabel('Xn (Volts)'); xlabel('n'); title(' Pulse Amplitude Spectrum [DC=1/2]'); grid on
figure(2)
stem(n,angle(y1)*180/pi,'b','LineWidth',1.4)
ylabel('Phase (Degrees)'); xlabel('n'); title('Pulse Phase Spectrum [DC=1/2]'); grid on
%{
figure(3)
stem(n,abs(y2),'r','LineWidth',1.4)
ylabel('Xn (Volts)'); xlabel('n'); title('Pulse Amplitude Spectrum [DC=1/5]'); grid on
figure(4)
stem(n,angle(y2)*180/pi,'b','LineWidth',1.4)
ylabel('Phase (Degrees)'); xlabel('n'); title(' Pulse Phase Spectrum [DC=1/5]'); grid on
i dont want to recreate my code and use loops, any ideas how to force those super small values to 0, so that they dont affect my phase diagram
  1 comentario
Benjamin Thompson
Benjamin Thompson el 2 de Feb. de 2022
You can use an index vector with ones in the spots that you want to replace with zero, similar to:
>> X
X =
1.0e-06 *
0.6555
0.1712
0.7060
0.0318
0.2769
>> I = (abs(X) < 1e-7)
I =
5×1 logical array
0
0
0
1
0
>> X(I) = 0
X =
1.0e-06 *
0.6555
0.1712
0.7060
0
0.2769

Iniciar sesión para comentar.

Respuesta aceptada

James Tursa
James Tursa el 2 de Feb. de 2022
Editada: James Tursa el 2 de Feb. de 2022
Instead of
if abs(y1)< 1e-6
y1=0;
end
try
y1(abs(y1)<1e-6) = 0;

Más respuestas (1)

Benjamin Thompson
Benjamin Thompson el 2 de Feb. de 2022
You can use an index vector with ones in the spots that you want to replace with zero, similar to:
>> X
X =
1.0e-06 *
0.6555
0.1712
0.7060
0.0318
0.2769
>> I = (abs(X) < 1e-7)
I =
5×1 logical array
0
0
0
1
0
>> X(I) = 0
X =
1.0e-06 *
0.6555
0.1712
0.7060
0
0.2769

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by