Find cases of vectors-patterns in an array

I have 4 cases of vectors inside an array that I want to index. All of them have trailing -1. I would like to index such patterns in my array.
Case 1
0 -1 -1 -1...-1 0 % starts with 0 ends with 0
Case 2
0 -1 -1 -1...-1 1 % starts with 0 ends with 1
Case 3
1 -1 -1 -1...-1 1 % starts with 1 ends with 1
Case 4
1 -1 -1 -1...-1 0 % starts with 1 ends with 0
An example of an array could be:
t=[0 0 0 0 0 *0 -1 1* 1 1 1 1 *1 -1 -1 -1 1* 0 0 0 0];
In this array case 2 and case 3 are observed in between the asterisks.
Thank you!

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 8월 17일
편집: Andrei Bobrov 2013년 8월 17일

1 개 추천

t=[0 0 0 0 0 0 -1 1 1 1 1 1 1 -1 -1 -1 1 0 0 0 0];
t1 = t(:).' == -1;
ii = [strfind(t1,[0 1]);strfind(t1,[1 0])+1].';
cases = [0 0;0 1;1 1;1 0];
[i1,i1] = ismember(t(ii),cases,'rows');
out = [i1, ii];

추가 답변 (1개)

Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013년 8월 17일
편집: Giorgos Papakonstantinou 2013년 8월 17일

0 개 추천

I had also this in my mind as a backup
idx= [0; diff(t1'==-1)]==-1 | [diff(t1'==-1); 0]==1;
a=reshape(t1(idx),2,[])
Then the vector cases you said above in combination with ismember would provide what I wanted.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by