Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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 vue (au cours des 30 derniers jours)
Diego Dranuta
Diego Dranuta le 26 Août 2019
Clôturé : MATLAB Answer Bot le 20 Août 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

Réponses (2)

David Hill
David Hill le 26 Août 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
Andrei Bobrov le 26 Août 2019
Modifié(e) : Andrei Bobrov le 26 Août 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;

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by