Getting the Indices of an Array with conditions

조회 수: 1 (최근 30일)
ARN
ARN 2018년 9월 12일
댓글: ARN 2018년 9월 12일
i have a program in which i have a matrix (Let's say A) of 0's and 1's. The occurance of 0's and 1's are random I want to get the indeces information for example
A1= [- - - - - - - - - 15:30:00 15:30:01 15:30:02 15:30:03 15:30:04 15:30:05 15:30:06 15:30:07 - - - - 15:30:12 15:30:13 15:30:14 15:30:15 15:30:16 15:30:17 - - - - - - 15:30:24 15:30:25 15:30:26 15:30:27 - - 15:30:30 15:30:31 - - ];
A= [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 ];
% i want the result vector to be like
B=[9 10 17 21 22 27 33 .....]
Conditions: 1. for all the zeros only one zero 2. for ones Starting and ending one
Or end result
B1= [0 15:30:00 15:30:07 0 15:30:12 15:30:17 0 15:30:24 15:30:27 0 15:30:30 15:30:31 0]
  댓글 수: 3
Jan
Jan 2018년 9월 12일
Please post working Matlab code. I cannot guess reliably, what
A1= [- - - - - - - - - 15:30:00 15:30:01 ...
is. A cell string? A string? A timeseries object, where - means a NaT?
The explanation "1. for all the zeros only one zero 2. for ones Starting and ending one" is not clear to me. Can you elaborate it a little bit? You mention two different wanted outputs:
B=[9 10 17 21 22 27 33 .....]
B1= [0 15:30:00 15:30:07 0 15:30:12 ...
What do you want exactly?
Rik
Rik 2018년 9월 12일
It looks like you should have a vector with true for each valid position and false for non-valid positions. Then you can use diff to find the alternation points and find the indices for your output.

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

채택된 답변

Jan
Jan 2018년 9월 12일
편집: Jan 2018년 9월 12일
A bold guess:
A = [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 ];
Ap = [0, A, 0]; % Padding to care for leading and trailing 1s
ini = strfind(Ap, [0,1]);
fin = strfind(Ap, [1,0]);
B = reshape([ini-1; ini; fin-1], 1, []);
  댓글 수: 1
ARN
ARN 2018년 9월 12일
I am sorry, i couldn't explain properly. This works Thanks

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by