Searching double patterns in a vector

When strfind is updated in a later version of Octave it should include double numbers also. Matlab:

    a = 1:10; a=a+.1
    a =
    1.1000 2.1000 3.1000 4.1000 5.1000 6.1000 7.1000 8.1000 9.1000 10.1000
    strfind(a, [2.1 3.1])
    ans =
    2

This works perfect in Matlab but not in Octave. Is there a workarround I haven’t found? Because I think this is a very helpful behavior even if it’s not documented. And a clever workaround would be highly appreciated, especially when this never will be implemented (according @Pantxo).

  • Windows 7
  • Octave Version 7.1
  • Installation method: e.g. Downloaded and installed “octave-7.1-w64-installer.exe” from Download
pat = [2.1 3.1];
find (a(1:end-1) == pat(1) & a(2:end) == pat(2))

Or for searching general patterns pat in vector a:

lgl = true (1, numel(a));
len = numel (pat);
for i = 1:len
  lgl &= (a(i:(end-len+i)) == pat(i));
end
find (lgl)

I’m dubious about searching for floating point with equality signs, so change it to abs (...) <= 1e-6 if needed.

Genial. Thank you.

Just a last remark: Your first suggestion works fine and is easy to expand for more patterns. The scond suggestion I had to modify a little bit, see below. Cause I got a nonconformace error and I want to have it ML-compatible. A review is welcome.

a = 1:10; a=a+.1; a =[a a]
pat = [ 4.1 5.1 6.1]

lgl = true (1, numel(a));
len = numel (pat);
for i = 1:len
  res = (a(i:(end-len+i)) == pat(i));
  lgl = lgl(1:length(res)) & res;
end

iNdex = find (lgl)

You’re right. I found a slightly faster way based on yours:

len = numel (pat);
lgl = (a(1:(end+1-len)) == pat(1);
for i = 2:len
    lgl = lgl & (a(i:(end-len+i)) == pat(i));
end
find (lgl)
1 Like