How to find position of array
Show older comments
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
2 Comments
madhan ravi
on 17 Jun 2019
[1 0 1 1 0 0 0 0 1 1 1 1] - the result would be?
Wind flower
on 20 Jun 2019
Accepted Answer
More Answers (1)
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])
3 Comments
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
Note that using numeric arrays as inputs to strfind is currently undocumented (and it even returns an error in GNU Octave). It would be nice if Mathworks changed the documentation. It would be even better if they extended this function to work on arrays of any object type.
Wind flower
on 20 Jun 2019
Edited: madhan ravi
on 20 Jun 2019
Categories
Find more on Data Type Conversion 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!