Asked by Wind flower
on 17 Jun 2019

For example i have data like this [11111100001111]

Now i need to find out the position of first 1 and last 1 of first group of 1s and then position of first 1 and last 1 of second group of 1s. How do i do this??

i have used' find' to find the position of all 1s but i dont want all...instead 1 want only the first and last position of groupof 1s

Answer by Debasish Samal
on 17 Jun 2019

Edited by Debasish Samal
on 17 Jun 2019

Accepted Answer

You can use regular expressions to do this.

x = '11111100001111';

expr = '1*';

[sInd,eInd] = regexp(x, expr)

This gives the following result:

sInd = 1×2

1 11

eInd = 1×2

6 14

You just need to convert the input data into a string.

madhan ravi
on 20 Jun 2019 at 9:24

Debasish Samal
on 20 Jun 2019 at 13:21

Wind flower
on 20 Jun 2019 at 15:11

@madhan ravi: Your answer was correct. Sorry first i got some error then i got to know. Your answer is simpler and correct. Sorry for the mistake

Thank You

Sign in to comment.

Answer by madhan ravi
on 17 Jun 2019

Simpler without any conversions:

s=[1 0 1 1 0 0 0 0 1 1 1 1]; % example data

x=s~=0;

Start = strfind([0,x],[0 1])

End = strfind([x,0],[1 0])

madhan ravi
on 17 Jun 2019

s = 11111100001111; % if your data is indeed like below then

m = floor(log10(s));

D = mod(floor(s ./ 10 .^ (m:-1:0)), 10);

x=D~=0;

Start = strfind([0,x],[0 1])

End = strfind([x,0],[1 0])

Rik
on 17 Jun 2019

Wind flower
on 20 Jun 2019 at 15:12

@madhan ravi : Thank You

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## madhan ravi (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/467406-how-to-find-position-of-array#comment_715404

## Wind flower (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/467406-how-to-find-position-of-array#comment_716539

Sign in to comment.