Index through 2 vectors to determine if 1 contains a subset of the other, return the index of each vector where they synchronize
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Liam
le 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 commentaires
Voss
le 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
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!