How to find the index location of repeated consecutive numbers over a tolerance within a vector

37 views (last 30 days)
For vector A, how would you find the index value for the start of a repeated consecutive value repeated more than x times
Example 1:
A = [4 2 7 4 4 7 9 9 3 8 8 8 8 8 8 5 6 6 7 ]
if x is 5 in this case I would like the answer to give 10.
Example 2:
For vector A attached using x = 7, would like the answer outputed to be 4249
Any sugesstions are much appreciated

Accepted Answer

David Hill
David Hill on 25 Feb 2021
This should work better. Did not previously think about multi-digit numbers.
n=7;%minimum number of repeats
a=num2str(~(diff(A)==0));
a=a(a~=' ');
idx=strfind(a,repmat('0',1,n-1));
  2 Comments
Lauren Crew
Lauren Crew on 1 Nov 2023
I hate to be that ignorant person, but I'm getting an error in strfind. Says "First argument must be text". But isn't text? I'm just trying to replicate this with my own repeated data. Thanks
Voss
Voss on 1 Nov 2023
@Lauren Crew: Can you save your A variable (or whatever variable you're trying to operate on) to a mat file and upload it here using the paperclip icon?
Also, please post the code you're running that gives the error.

Sign in to comment.

More Answers (1)

David Hill
David Hill on 25 Feb 2021
Likely lots of other ways.
n=5;%minimum number of repeats
idx=strfind(cell2mat(regexp(num2str(diff(A)),'[^- ]','match')),repmat('0',1,n-1));%idx(1) would be the first occurrance
  1 Comment
Nicholas Hero
Nicholas Hero on 25 Feb 2021
Thanks David, this method seems to work on a small vector but is somehow tripping up when used with a larger vector, have attached the vector A into the question, using n = 7 it is outputing index values which are not in the vector.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!