Clear Filters
Clear Filters

Index through 2 vectors to determine if 1 contains a subset of the other, return the index of each vector where they synchronize

1 view (last 30 days)
I have 2 large vectors of differing sizes, technically, one vector should contain a subset of the other, but I need to prove that out. Prior to synchronizing, each vector will have non-matching length of buffer data before they line up. A small example would look like:
set1 = [9 7 9 5 1 2 3 4 5];
set2 = [8 6 1 2 3 4 5];
I would like to return the index of each vector that points to where synchronization begins.

Answers (1)

Liam
Liam on 7 Mar 2024
Assuming the buffer is only at the start of each vector, something like this should work.
[a,b] = synchindex(set1,set2)
function [a,b] = synchindex(set1,set2)
a = length(set1);
b = length(set2);
if set1(a) ~= set2(b)
a = ('no match');
b = ('no match');
else if set1(a) == set2(b)
while set1(a-1) == set2(b-1)
a = a - 1;
b = b - 1;
end
else
a = ('no match');
b = ('no match');
end
end
end
  2 Comments
Voss
Voss on 7 Mar 2024
set1 = [9 7 9 5 1 2 3 4 5];
set2 = [8 6 1 2 3 4 5];
[a,b] = synchindex_modified(set1,set2)
a = 5
b = 3
[a,b] = synchindex_original(set1,set2)
a = 5
b = 3
set1 = [9 7 9 5 1 2 3 4 5];
set2 = [8 6 1 2 3 4 6];
[a,b] = synchindex_modified(set1,set2)
a = NaN
b = NaN
[a,b] = synchindex_original(set1,set2)
a = 'no match'
b = 'no match'
set1 = [1 2 3 4 5];
set2 = [1 2 3 4 5];
[a,b] = synchindex_modified(set1,set2)
a = 1
b = 1
[a,b] = synchindex_original(set1,set2)
Array indices must be positive integers or logical values.

Error in solution>synchindex_original (line 24)
while set1(a-1) == set2(b-1)
function [a,b] = synchindex_original(set1,set2)
a = length(set1);
b = length(set2);
if set1(a) ~= set2(b)
a = ('no match');
b = ('no match');
else
if set1(a) == set2(b)
while set1(a-1) == set2(b-1)
a = a - 1;
b = b - 1;
end
else
a = ('no match');
b = ('no match');
end
end
end
function [a,b] = synchindex_modified(set1,set2)
a = numel(set1);
b = numel(set2);
if set1(a) ~= set2(b)
a = NaN;
b = NaN;
return
end
while a > 1 && b > 1 && set1(a-1) == set2(b-1)
a = a - 1;
b = b - 1;
end
end

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!