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 次查看(过去 30 天)
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

回答(2 个)

David Hill
David Hill 2019-8-26
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 2019-8-26
编辑:Andrei Bobrov 2019-8-26
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;

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by