rounding issues in matlab, need to force values to 0
5 views (last 30 days)
Show older comments
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 Comment
Benjamin Thompson
on 2 Feb 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
Accepted Answer
James Tursa
on 2 Feb 2022
Edited: James Tursa
on 2 Feb 2022
Instead of
if abs(y1)< 1e-6
y1=0;
end
try
y1(abs(y1)<1e-6) = 0;
0 Comments
More Answers (1)
Benjamin Thompson
on 2 Feb 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
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!