How to create zeros between ones in array
显示 更早的评论
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
采纳的回答
更多回答(1 个)
Andrei Bobrov
2014-6-30
B = zeros(size(A));
B([strfind([0 A],[0 1]),strfind([A 0],[1 0])]) = 1;
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!