How to extract the first and last position for each ones series ?

조회 수: 1 (최근 30일)
For example
How to extract the first and last position for each ones series ?
X= [0 0 1 1 1 0 1 1 0 1 1 1 0 0 1 ]
%result
y =
3 5
7 8
10 12
15 15
Thanks
  댓글 수: 1
Cedric
Cedric 2013년 1월 23일
If it is for indexing something else afterwards, you can use that almost directly for logical indexing; you just have to typecast it to logical. E.g.
>> X = [0 0 1 1 1 0 1 1 0 1 1 1 0 0 1 ] ;
>> a = 1:15 ;
>> a(logical(X))
ans =
3 4 5 7 8 10 11 12 15

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 1월 23일
y = [strfind([~X(1) X],[0 1]);strfind([X ~X(end)],[1 0])]';

추가 답변 (3개)

per isakson
per isakson 2013년 1월 23일
Try
find( diff( X ) == 1 ) + 1
find( diff( X ) == -1 )
  댓글 수: 2
per isakson
per isakson 2013년 1월 23일
I've provided more than half of the solution. Your turn.
Javier
Javier 2013년 1월 23일
Thanks per........

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2013년 1월 23일
f = find([false,diff(x)~=0,false]);
y = [f(1:2:end)',f(2:2:end)'-1];

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 23일
You can use
x= [ 0 0 1 1 1 0 1 1 0 1 1 1 0 0 1 ]
y=[x(1) diff(x)] % to find the first column
x1=fliplr(x) % to find the second column by the first method, just by
% reversing x

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by