Detect sequence of numbers in array
31 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Brendan Lyden
am 17 Mär. 2017
Beantwortet: Jan
am 17 Mär. 2017
I want to be able to detect when a sequence of numbers starts and finishes in an array. For example, if I have
A = [1 0 1 0 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0];
B = [1 1 0 0];
Can I determine whether A contains B and if A does contain B, get the index of A that B starts and stops (so 7 and 18 here)
Thank you
0 Kommentare
Akzeptierte Antwort
the cyclist
am 17 Mär. 2017
Bearbeitet: the cyclist
am 17 Mär. 2017
strfind(A,B)
will find the starting indices where B is embedded in A.
It is not perfectly clear to me how you want to handle repetitions, or repetitions with gaps, etc. So, maybe this is enough to get you started?
0 Kommentare
Weitere Antworten (1)
Jan
am 17 Mär. 2017
The sequence is found at the positions 7, 11 and 15. If you search for "7, 18", I guees you want:
Index = strfind(A, B);
Result = [Index(1), Index(end) + length(b) - 1];
A fallback is required, when no match is found.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!