Cutting a certain pattern from an array
6 次查看(过去 30 天)
显示 更早的评论
Hello everyone!
I have a simple question regarding deleting certain pattern from array. For example I have a random array
A=[1 2 3 4 5 6 0 0 0 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0 ...]
I want to delete all fragments, built of four zeroes. So cut away all B=[0 0 0 0] from a 1d array.
How can I implement it?
0 个评论
采纳的回答
C.J. Harris
2015-9-23
A = [1 2 3 4 5 6 0 0 0 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0];
B = [0 0 0 0];
C = A(setxor(cell2mat(arrayfun(@(x)(x:x+length(B)-1),strfind(A,B), 'UniformOutput', false)), 1:length(A)));
2 个评论
更多回答(1 个)
Stephen23
2015-9-23
编辑:Stephen23
2015-9-23
>> A = [1,2,3,4,5,6,0,0,0,0,1,2,3,4,5,6,7,8,9,10,0,0,0,0];
>> B = [0,0,0,0];
>> +strrep(char(A),char(B),'')
ans =
1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10
2 个评论
Walter Roberson
2015-9-24
Hee, cute.
Works fine for non-negative integers up to 65535 but not in general.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!