Getting the Indices of an Array with conditions

1 次查看(过去 30 天)
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, []);

更多回答(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