# Numerical precision when using colon operator

3 Ansichten (letzte 30 Tage)
Xingwang Yong am 2 Apr. 2022
Kommentiert: Stephen23 am 2 Apr. 2022
I am doing simulation, which basically is iteration over time. Given the delta time dt and total time inter_pulse_space*num_pulse, the full time frame time1 can be easily calculated. I want to observe signal at time2 only. Surprisingly, time2 is not a subset of time1. Is this due to numerical precision?
clear
dt = 0.1/1e3; % 0.1 millisecond
num_pulse = 100;
inter_pulse_space = 40*dt;
time1 = dt:dt:inter_pulse_space*num_pulse;
time2 = inter_pulse_space/2 + (0:num_pulse-1)*inter_pulse_space;
ismember(time2,time1)
format long
time1(180)
time2(5)
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

KSSV am 2 Apr. 2022
You cannot use ismember for floating point numbers. If you want to have values at time2, you can use interp1. Read about interp1.
If you want to comapre floating point numbers refer:
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Stephen23 am 2 Apr. 2022
You certainly can use ISMEMBER for floating point numbers.
If you understand the finite precision of binary floating point numbers and expect/require exact matches only, then there is absolutely nothing stopping you from using ISMEMBER.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by