How to extract the first and last position for each ones series ?
Show older comments
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 Comment
Cedric
on 23 Jan 2013
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
Accepted Answer
More Answers (3)
per isakson
on 23 Jan 2013
Try
find( diff( X ) == 1 ) + 1
find( diff( X ) == -1 )
2 Comments
per isakson
on 23 Jan 2013
I've provided more than half of the solution. Your turn.
Javier
on 23 Jan 2013
Roger Stafford
on 23 Jan 2013
f = find([false,diff(x)~=0,false]);
y = [f(1:2:end)',f(2:2:end)'-1];
Azzi Abdelmalek
on 23 Jan 2013
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
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!