Info

# 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 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:
0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0
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

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

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]);
or
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;