Arrays 在没有循环的数组中查找由4个值组成的序列
我试图在数组中找到一个升序值序列,其中4个值没有for或if。 例如,A=[89137189],输入将是:13718 没有for的任何方法都可以使用strfind,不仅可以在字符串中查找模式,还可以在数字数组中查找模式。您要寻找的模式是三个连续的正差异:Arrays 在没有循环的数组中查找由4个值组成的序列,arrays,matlab,Arrays,Matlab,我试图在数组中找到一个升序值序列,其中4个值没有for或if。 例如,A=[89137189],输入将是:13718 没有for的任何方法都可以使用strfind,不仅可以在字符串中查找模式,还可以在数字数组中查找模式。您要寻找的模式是三个连续的正差异: A = [8 9 1 3 7 18 19] sequenceLength = 4; startIdx = strfind( sign(diff(A)), ones(1,sequenceLength-1)); sequences = A(b
A = [8 9 1 3 7 18 19]
sequenceLength = 4;
startIdx = strfind( sign(diff(A)), ones(1,sequenceLength-1));
sequences = A(bsxfun(@plus,startIdx',0:sequenceLength-1))
sequences =
1 3 7 18
3 7 18 19
注意:strfind查找重叠的间隔。如果需要独占间隔,可以查看regexp。strfind允许您不仅在字符串中查找模式,还可以在数字数组中查找模式。您要寻找的模式是三个连续的正差异:
A = [8 9 1 3 7 18 19]
sequenceLength = 4;
startIdx = strfind( sign(diff(A)), ones(1,sequenceLength-1));
sequences = A(bsxfun(@plus,startIdx',0:sequenceLength-1))
sequences =
1 3 7 18
3 7 18 19
注意:strfind查找重叠的间隔。如果需要独占间隔,可以查看regexp。这将为您提供所有此类子序列的起始索引:
n = 4;
indices = find(conv(double(diff(A)>0), ones(1,n-1), 'valid')==n-1);
例如:
A = [8 9 1 3 7 18 9 10 11 12 5];
产生
indices =
3 7
因此子序列将是Aindices1+0:n-1,Aindices2+0:n-1,等等:
这将为您提供所有此类子序列的起始索引:
n = 4;
indices = find(conv(double(diff(A)>0), ones(1,n-1), 'valid')==n-1);
例如:
A = [8 9 1 3 7 18 9 10 11 12 5];
产生
indices =
3 7
因此子序列将是Aindices1+0:n-1,Aindices2+0:n-1,等等:
另一个解决方案:
A = [8 9 1 3 7 18 9 10 11 12 5];
len = 4;
subseqs = hankel(A(1:len), A(len:end));
idx = all(diff(subseqs) > 0);
out = subseqs(:,idx);
另一个解决方案:
A = [8 9 1 3 7 18 9 10 11 12 5];
len = 4;
subseqs = hankel(A(1:len), A(len:end));
idx = all(diff(subseqs) > 0);
out = subseqs(:,idx);
很好地看到了bsxfun部分很好地看到了bsxfun部分