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)
Show older comments
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.
0 Comments
Answers (1)
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
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,b] = synchindex_original(set1,set2)
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,b] = synchindex_original(set1,set2)
set1 = [1 2 3 4 5];
set2 = [1 2 3 4 5];
[a,b] = synchindex_modified(set1,set2)
[a,b] = synchindex_original(set1,set2)
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
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!