This question is closed. Reopen it to edit or answer.

How can I code so: if a value on an array is false(0), and there is another false later on the list,all the 1's between (true) all the 1's turn into 0?

1 view (last 30 days)
Diego Dranuta
Diego Dranuta on 26 Aug 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
After processing some data I have ended up with a boolean. Now I want to gropup different sets of data by 0's and 1's but if there is only a few rows between one 1 and the other, i want to put them together in the same group.
For instance, I have this list:
How can I code so it looks like this after:0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0
Lets say that the rule would be that i moves and it looks 3 rows before and after, if there is a 1 with in the 1 next of previous rows then those 0's in between turn into 1's.
Any idea is much appreciated

Answers (2)

David Hill
David Hill on 26 Aug 2019
c=find(b<4);%whatever you want for the number of zeros between 1's that you want to change
for i=c
The above should do what you want if I understood you correctly.

Andrei Bobrov
Andrei Bobrov on 26 Aug 2019
Edited: Andrei Bobrov on 26 Aug 2019
A = [0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0];
imerode(imdilate(A,[1 1 1]),[1 1 1]);
c = cumsum(A);
cc = cumsum(A,'reverse');
C = (c & cc).*c;
b = accumarray(C(:)+1,1);
b = b(2:end);
ii = (1:numel(b))';
A(ismember(C,ii(b < 4))) = 1;

Community Treasure Hunt

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

Start Hunting!