Find where certain sequence of true/falses is placed inside a logical array

8 Ansichten (letzte 30 Tage)
Hello!
Assume that I have the following logical array:
x=[false;false;true;true;true;true;true;false;true;true;true]'
x = 1×11 logical array
0 0 1 1 1 1 1 0 1 1 1
I would like to construct a logical array which returns true at the index where the sequence [true;false;true] is found
Hence the result would look like this
result=[false;false;false;false;false;false;true;false;false;false;false]'
result = 1×11 logical array
0 0 0 0 0 0 1 0 0 0 0
Hope that the question is clear, thank you!

Akzeptierte Antwort

David Hill
David Hill am 13 Okt. 2022
x=[false;false;true;true;true;true;true;false;true;true;true]';
f=strfind(x,[1 0 1]);
result=zeros(size(x));
result(f)=1
result = 1×11
0 0 0 0 0 0 1 0 0 0 0
  1 Kommentar
Enrico Gambini
Enrico Gambini am 13 Okt. 2022
Bearbeitet: Enrico Gambini am 13 Okt. 2022
Thank you!
For my purposes I would slightly change the code like this:
x=[false;false;true;true;true;true;true;false;true;true;true]';
f=strfind(x,[1 0 1]);
result=false(size(x));
result(f)=true
result = 1×11 logical array
0 0 0 0 0 0 1 0 0 0 0

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by