假设一个向量是x= -2 -2 -1 -1 -1 -1 -2 -1 0 5 -1 0 5-10,另一个向量是y= 2 3 4 5 -1 0 5 -1 0 5 -1 0 5-1。这两个向量不需要具有相同的长度。我想用MATLAB找出两个向量中最长连续数的相似序列/模式。结果应该是两个向量中匹配模式的开始和结束索引。对于本例: ix=7 12和iy=5 10。
发布于 2019-11-12 04:06:37
这需要图像处理工具箱和统计工具箱。它在块大小上使用循环:
x = [-2 -2 -1 -1 -1 -2 -1 0 5 -1 0 5 -1 0];
y = [ 2 3 4 5 -1 0 5 -1 0 5 -1];
for n = min(numel(x), numel(y)):-1:1; % try sizes in decreasing order
x_sliding = reshape(im2col(x,[1 n],'sliding'),n,[]).'; % reshape needed for n=1
y_sliding = reshape(im2col(y,[1 n],'sliding'),n,[]).'; % reshape needed for n=1
[ind_x, ind_y] = find(pdist2(x_sliding, y_sliding) == 0);
if ~isempty(ind_x)
ix_start = ind_x;
iy_start = ind_y;
ix_end = ind_x+n-1;
iy_end = ind_y+n-1;
break
end
end解决方案(如果存在)在ix_start、ix_end、iy_start、iy_end中提供。如果存在几个最大可能大小的解,则产生所有这些解的索引。
https://stackoverflow.com/questions/58807002
复制相似问题