Comparing elements of two uneven lists of numbers

3 views (last 30 days)
I am trying to compare numbers in two different lists to see if 2 subjects started the same action within 10 seconds of eachother. For example, I have one list of numbers
15
420
5778
6018
6388
and another list
414
1430
5762
6384
7098
What I would like to do is see if any number in list 2 is within 10 seconds of any number in list one.
For example, 420 and 414 are within 10 seconds of eachother, so I would like to take these numbers and mark the index of each number in its respective list.
The goal is to get the indexes I need for each list individually, and remove all of the other rows of each list seperately so only the correct numbers remain. In the above example the new lists would be:
420 and 414
6388 6384
The goal is for both lists to be the same length in the end. The data I am using here is time, so there is no chance of a number occuring more than once, and numbers are always in ascending order.

Answers (1)

Chunru
Chunru on 21 Sep 2021
a1=[ 15
420
5778
6018
6388 ];
a2 = [ 414
1430
5762
6384
7098 ];
idx = find(abs(a1 - a2') <=10)
idx = 2×1
2 20
[i, j] = ind2sub([length(a1), length(a2)], idx)
i = 2×1
2 5
j = 2×1
1 4
% a1(2) and a2(1)
% a1(5) and a2(4)

Tags

Community Treasure Hunt

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

Start Hunting!