Index through 2 vectors to determine if 1 contains a subset of the other, return the index of each vector where they synchronize
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Liam
2024-3-7
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 个评论
Voss
2024-3-7
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!