How to create zeros between ones in array

1 回表示 (過去 30 日間)
Samuel
Samuel 2014 年 6 月 30 日
回答済み: Andrei Bobrov 2014 年 6 月 30 日
hello,
anyone know a fast way to create zeros between ones in an array?
for example A = [0 0 0 1 1 1 1 0 0 0]
I want it to return B = [0 0 0 1 0 0 1 0 0 0]
I was thinking of doing a double for loop running from the left side and the right. If there is a better way, please let me know. Thanks in advance

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 30 日
A=[0 0 0 1 1 1 1 0 0 0]
AA=[0 A 0]
i1=strfind(AA,[0 1])+1
i2=strfind(AA,[1 0])-2
for k=1:numel(i1)
A(i1:i2)=0;
end
A

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 6 月 30 日
B = zeros(size(A));
B([strfind([0 A],[0 1]),strfind([A 0],[1 0])]) = 1;

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by