issue with 'intersect' function
Mostra commenti meno recenti
Hello, I have defined a vector t=[0,1] with dt=0.0001. Now, I consider another time interval DT=100*dt=0.01. I then use command [val, ind]=intersect(t,t1,'stable'), to find the indices of the common elements between t and t1. At the same time, I define another vector t2=0:DT:1 and t3=t(ind). Now I expect that t2 and t3 should be identical. However, t3 is missing some elements. Here is my code:
dt=0.0001; t=0:dt:1; % original vector
R=100; DT=R*dt; %new step size
t1=0:DT:t(end); %vector based on new step
[sv, ind]=intersect(t,t1); % find indices of common elements
t2=t(ind); % new vector created using indices
figure()
p1=plot(t,t,'k.') %plot original vector
hold on
p2=plot(t1,t1,'o') % vector defined using DT
p3=plot(t2,t2,'sq') % vector created using indices from intersect. Notice p2 ~= p3.

Risposta accettata
Più risposte (1)
Rik
il 23 Gen 2022
0 voti
Decimals are difficult for computers, since they work in binary, not base 10.
Say I ask you to divide 1 by 3. You answer 0.3333. Then I ask you to multiply the result by 3. Should you reply 1 or 0.9999? Since you wrote down the result with a finite precision you should answer the second option.
Now you select the list of integers. Is 0.9999 an integer? It isn't, but based on the calculation steps you might have expected it to be there.
The moral of the story: always use a tolerance when dealing with non-integers. You can probably use the ismembertol function to achieve what you need, or generate the new indices by hand like you did.
1 Commento
9times6
il 23 Gen 2022
Categorie
Scopri di più su Data Type Identification in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!