Find x values where y values are the same from a set of data.

I have 2 datasets from a continuous signal that will display a voltage in 1 set of data (D8) and repeat that voltage in data set D2 after a randomized delay. I need to find the voltage and time these repetitions occur, but I'm unsure of how to do that.
My code finds these repetitions, removes where the 2 datasets intercept as there's no delay going on there, and displays the time of the repetitions. I need the original voltage, the original time from D8, and the delayed time in D2 when the repeated value occurs.
data = load('DataSet2_Test.txt'); %my directory is different but it includes my name so it just calls the attached file
%Characterize columns
t1 = data(:,1)/1000; %convert time from ms to s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1 = (data(:,2)*5)/resolution*10^8; D2 = (data(:,3)*5)/resolution*10^8;
D3 = (data(:,4)*5)/resolution*10^8; D4 = (data(:,5)*5)/resolution*10^8;
D5 = (data(:,6)*5)/resolution*10^8; D6 = (data(:,7)*5)/resolution*10^8;
D7 = (data(:,8)*5)/resolution*10^8; D8 = (data(:,9)*5)/resolution*10^8; %all in V
c1 = D8(:)==D2 %finds same voltage indep of time occuring
c1 = 120×1 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
x1values1 = t1(find(c1)); %Gives time of repeated values
r1usure1 = t1(find(intersect(D8,D2))); %checks for intersections as they don't include delay
l1 = setdiff(x1values1, r1usure1); %Gives time of repeated values excluding intersections
This is the code I have so far, but it only checks for similar values and returns repeated time values instead of including the original time, and I've been struggling to find the associated voltages. I have also attached the file with the signals where the first column is time, column 3 is D2 in V, and column 9 is D8 in V.
This might not be well explained so I will answer questions as quickly as possible. Thank you in advance !

1 Comment

You are testing for exact equality between the two signals. If they are continuous signals with noise, their time samples would never normally coincide. Are you actually trying to find the delay between two pulses? For that, you would normally use xcorr. However, It doesn't look like there is any lag between D8 and D2. Their peaks pretty much coincide --
data = load('DataSet2_Test.txt'); %my directory is different but it includes my name so it just calls the attached file
%Characterize columns
t1 = data(:,1)/1000; %convert time from ms to s
%convert from bits to V
resolution = 2^10-1;
D=data(:,2:end)*5/resolution*1e8;
plot(t1,D(:,8),t1,D(:,2)); axis padded
legend('D8','D2', Location = 'best'); xlabel t; ylabel D

Sign in to comment.

 Accepted Answer

First column is voltage from D8 (multiplied by 1e-8), the following columns are the times this voltage repeats in D2 afterwards.
data = load('DataSet2_Test.txt'); %my directory is different but it includes my name so it just calls the attached file
%Characterize columns
t1 = data(:,1)/1000; %convert time from ms to s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1 = (data(:,2)*5)/resolution*10^8; D2 = (data(:,3)*5)/resolution*10^8;
D3 = (data(:,4)*5)/resolution*10^8; D4 = (data(:,5)*5)/resolution*10^8;
D5 = (data(:,6)*5)/resolution*10^8; D6 = (data(:,7)*5)/resolution*10^8;
D7 = (data(:,8)*5)/resolution*10^8; D8 = (data(:,9)*5)/resolution*10^8; %all in V
for i = 1:length(D8)
T = [];
compare = D8(i);
T(1) = compare*1e-8;
index = 1;
for j = i+1:length(D2)
if abs(D2(j) - compare) < 1e-8
index = index + 1;
T(index) = t1(j);
end
end
full_T{i} = T;
end
full_T{1:10}
ans = 1×3
4.5308 209.2480 209.4990
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×3
4.4379 214.4190 215.8370
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×2
4.4966 209.5820
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×5
4.4526 213.6680 214.5860 216.0870 216.5880
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×5
4.4037 211.5000 211.7500 212.1670 212.5840
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×6
4.3646 210.0820 210.4160 210.4990 210.5830 213.1680
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 4.4673
ans = 1×5
4.4037 211.5000 211.7500 212.1670 212.5840
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×2
4.4721 216.8380
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×6
4.4135 209.8320 211.0830 213.9180 215.0030 215.4190
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

10 Comments

This is great, thank you so much!
Small follow-up question, how would I find the original time value for the repeated voltage?
Like this ? First column is voltage from D8 (multiplied by 1e-8), second column is original time and the following columns are the times the voltage repeats in D2 afterwards.
data = load('DataSet2_Test.txt'); %my directory is different but it includes my name so it just calls the attached file
%Characterize columns
t1 = data(:,1)/1000; %convert time from ms to s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1 = (data(:,2)*5)/resolution*10^8; D2 = (data(:,3)*5)/resolution*10^8;
D3 = (data(:,4)*5)/resolution*10^8; D4 = (data(:,5)*5)/resolution*10^8;
D5 = (data(:,6)*5)/resolution*10^8; D6 = (data(:,7)*5)/resolution*10^8;
D7 = (data(:,8)*5)/resolution*10^8; D8 = (data(:,9)*5)/resolution*10^8; %all in V
for i = 1:length(D8)
T = [];
compare = D8(i);
T(1) = compare*1e-8;
T(2) = t1(i);
index = 2;
for j = i+1:length(D2)
if abs(D2(j) - compare) < 1e-8
index = index + 1;
T(index) = t1(j);
end
end
full_T{i} = T;
end
full_T{1:10}
ans = 1×4
4.5308 208.9980 209.2480 209.4990
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×4
4.4379 209.0820 214.4190 215.8370
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×3
4.4966 209.1650 209.5820
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×6
4.4526 209.2480 213.6680 214.5860 216.0870 216.5880
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×6
4.4037 209.3320 211.5000 211.7500 212.1670 212.5840
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×7
4.3646 209.4150 210.0820 210.4160 210.4990 210.5830 213.1680
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×2
4.4673 209.4990
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×6
4.4037 209.5820 211.5000 211.7500 212.1670 212.5840
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×3
4.4721 209.6660 216.8380
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1×7
4.4135 209.7480 209.8320 211.0830 213.9180 215.0030 215.4190
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note that if voltages repeat in D8, times taken from D2 will also repeat - only within a shorter list. I don't know if this makes sense for your application.
You are so awesome, thank you! I'm also struggling to find the change between the initial occurence and the first repeated value. I am using T(2) for the first occurence but I'm not certain on how to index the repeated value.
full_T{i}(1)
is D8(i), multiplied by 1e-8,
full_T{i}(2)
is t1(i) (the original time) and
full_T{i}(3:end)
are the times D8(i) repeats in D2 after t1(i).
data = load('DataSet2_Test.txt'); %my directory is different but it includes my name so it just calls the attached file
%Characterize columns
t1 = data(:,1)/1000; %convert time from ms to s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1 = (data(:,2)*5)/resolution*10^8; D2 = (data(:,3)*5)/resolution*10^8;
D3 = (data(:,4)*5)/resolution*10^8; D4 = (data(:,5)*5)/resolution*10^8;
D5 = (data(:,6)*5)/resolution*10^8; D6 = (data(:,7)*5)/resolution*10^8;
D7 = (data(:,8)*5)/resolution*10^8; D8 = (data(:,9)*5)/resolution*10^8; %all in V
for i = 1:length(D8)
T = [];
compare = D8(i);
T(1) = compare*1e-8;
T(2) = t1(i);
index = 2;
for j = i+1:length(D2)
if abs(D2(j) - compare) < 1e-8
index = index + 1;
T(index) = t1(j);
end
end
full_T{i} = T;
end
full_T{2}(1)
ans = 4.4379
full_T{2}(2)
ans = 209.0820
full_T{2}(3:end)
ans = 1×2
214.4190 215.8370
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Thank you so much, I really appreciate you breaking it down for me too!
Probably last question, but I'm trying to calculate the first repeated time subtract the original time for time of flight, but with the double indexing it only calculates for 1 value instead of all the times it happens in the data. How can I calculate every time of flight of each repeated voltage? Here's my code, including the part that gives me errors!
data = load('DataSet2_Test.txt');
%Characterize columns
t1 = data(:,1)/1000; % time s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1 = (data(:,2)*5)/resolution; D2 = (data(:,3)*5)/resolution;
D3 = (data(:,4)*5)/resolution; D4 = (data(:,5)*5)/resolution;
D5 = (data(:,6)*5)/resolution; D6 = (data(:,7)*5)/resolution;
D7 = (data(:,8)*5)/resolution; D8 = (data(:,9)*5)/resolution;
%Test 1
for ap = 1:length(D8)
T = [];
compare = D8(ap)
T(1) = compare;
T(2) = t1(ap);
index = 2;
for bp = ap+1:length(D2)
if abs(D2(bp)-compare) < 1e-8
index = index + 1;
T(index) = t1(bp);
end
end
full_T{ap} = T
end
%Part I'm struggling with
%for 1 value
V_repeat = full_T{2}(1); %V
t_initial = full_T{2}(2); %t
t_repeat = full_T{2}(3); %t
tof = t_repeat-t_initial; %t
%for every V repeated
V_rep = full_T{:}(1)
t_in = full_T{:}(2)
t_rep = full_T{:}(3)
tofall = t_rep - t_in
I'm not sure if this is what you want. I set tofall(i) to full_T{i}(3)-full_T{i}(2) if the voltage repeats and to NaN else.
data = load('DataSet2_Test.txt');
%Characterize columns
t1 = data(:,1)/1000; % time s
%convert from bits to V
resolution = 2^10-1;
%Data as assigned pairs *10^8 to transfer within the MeV to GeV range
D1 = (data(:,2)*5)/resolution; D2 = (data(:,3)*5)/resolution;
D3 = (data(:,4)*5)/resolution; D4 = (data(:,5)*5)/resolution;
D5 = (data(:,6)*5)/resolution; D6 = (data(:,7)*5)/resolution;
D7 = (data(:,8)*5)/resolution; D8 = (data(:,9)*5)/resolution;
%Test 1
for ap = 1:length(D8)
T = [];
compare = D8(ap);
T(1) = compare;
T(2) = t1(ap);
index = 2;
for bp = ap+1:length(D2)
if abs(D2(bp)-compare) < 1e-8
index = index + 1;
T(index) = t1(bp);
end
end
full_T{ap} = T;
end
%Part I'm struggling with
%for 1 value
V_repeat = full_T{2}(1); %V
t_initial = full_T{2}(2); %t
t_repeat = full_T{2}(3); %t
tof = t_repeat-t_initial; %t
%for every V repeated
for i = 1:length(D8)
T = full_T{i};
if numel(T) > 2
V_rep(i) = T(1);
t_in(i) = T(2);
t_rep(i) = T(3);
tofall(i) = t_rep(i) - t_in(i);
else
V_rep(i) = T(1);
t_in(i) = T(2);
t_rep(i) = NaN;
tofall(i) = NaN;
end
end
tofall
tofall = 1×120
0.2500 5.3370 0.4170 4.4200 2.1680 0.6670 NaN 1.9180 7.1720 0.0840 1.5020 1.0010 0.3340 0.2500 0.9180 2.1680 0.6680 NaN 1.1670 3.0850 3.7530 3.0860 3.3360 4.5870 NaN 0.5830 3.5860 1.1670 0.2500 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
That is perfect, thank you!

Sign in to comment.

More Answers (1)

Looking at your plot, the D1 and D8 signals look similar but not the same - not exactly identical like it was just copied to a new location. So I would use cross correlation to find the separation. Try xcorr or normalized cross correlation normxcorr2 (not sure if that one works with 1-D signals). I think there is an example showing how to find the lag/separation between two signals.
help xcorr
xcorr - Cross-correlation Syntax r = xcorr(x,y) r = xcorr(x) r = xcorr(___,maxlag) r = xcorr(___,scaleopt) [r,lags] = xcorr(___) Input Arguments x - Input array vector | matrix | multidimensional array y - Input array vector maxlag - Maximum lag integer scalar scaleopt - Normalization option 'none' (default) | 'biased' | 'unbiased' | 'normalized' | 'coeff' Output Arguments r - Cross-correlation or autocorrelation vector | matrix lags - Lag indices vector Examples openExample('matlab/CrossCorrelationOfTwoVectorsExample') openExample('matlab/AutocorrelationOfVectorExample') openExample('matlab/NormalizedCrossCorrelationExample') See also conv, corrcoef, cov, xcov Introduced in MATLAB before R2006a Documentation for xcorr doc xcorr Other uses of xcorr gpuArray/xcorr tall/xcorr

Asked:

on 12 May 2026

Commented:

on 22 May 2026

Community Treasure Hunt

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

Start Hunting!