MATLAB: Which is the shortest/simplest way to check a subvector in a vector
matchvector
For example I would like to check the existence of [1,5] in a larger vector: [2,3,1,5,6,6,1]
Best Answer
Interestingly you can usestrfind to locate patterns in numeric vectors as well as in strings. Here are your two examples, the first with a match, the second without:
>> strfind([2,3,1,5,6,6,1], [1,5])
ans =
3
>> strfind([2,3,1,6,5,6,1], [1,5])
ans =
[]
To turn this into a logical value, simply wrap this with~isempty:
See the documentation for reshape for details. The empty square brackets there indicate that MATLAB should infer the length of the first dimension from the size of X (just as you want).
Best Answer