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

madhan ravi
madhan ravi 2019년 6월 17일
[1 0 1 1 0 0 0 0 1 1 1 1] - the result would be?
Wind flower
Wind flower 2019년 6월 20일
for my data [11111100001111], the answer would be 1 and 6 for first set , 11 and 14 for the second set. Afer that i want to save that in array like this:
1 6
11 14

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

 채택된 답변

Debasish Samal
Debasish Samal 2019년 6월 17일
편집: Debasish Samal 2019년 6월 17일

1 개 추천

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.

댓글 수: 6

madhan ravi
madhan ravi 2019년 6월 17일
Don't ever name a variable as exp it would hinder the in-built function exp().
Debasish Samal
Debasish Samal 2019년 6월 17일
Thank You. Point noted :-)
Wind flower
Wind flower 2019년 6월 20일
@Debasish Samal your answer is good. But i have that data in column form in excel and reading it using xlsread so how will i convert that into a string (My actual data is very very big)
madhan ravi
madhan ravi 2019년 6월 20일
편집: madhan ravi 2019년 6월 20일
Wind flower: Did you see the answer below? I don’t know why my answer was marked Accepted mistakenly.
Debasish Samal
Debasish Samal 2019년 6월 20일
편집: Debasish Samal 2019년 6월 20일
@Wind flower as far as I know xlsread returns a matrix of integers(if you are reading integers). So very basic approach would be using num2str to convert the num type to string type. Then iterate over each cell to get the result. I am not sure how your data looks like after reading it using xlsread. But I am sure you can figure out a way to optimise the conversion and get the desired result. Thanks.
Wind flower
Wind flower 2019년 6월 20일
@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

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 6월 17일

2 개 추천

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

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
Rik 2019년 6월 17일
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
Wind flower 2019년 6월 20일
편집: madhan ravi 2019년 6월 20일
@madhan ravi : Thank You

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

카테고리

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

태그

질문:

2019년 6월 17일

편집:

2019년 6월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by