Getting the Indices of an Array with conditions

1 view (last 30 days)
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 Comments
Jan
Jan on 12 Sep 2018
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 on 12 Sep 2018
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.

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Sep 2018
Edited: Jan on 12 Sep 2018
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, []);

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!