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 日間)
Diego Dranuta
Diego Dranuta 2019 年 8 月 26 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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