Cutting a certain pattern from an array

조회 수: 1 (최근 30일)
beatlewalrus
beatlewalrus 2015년 9월 23일
댓글: Stephen23 2015년 9월 24일
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?

채택된 답변

C.J. Harris
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
Stephen23
Stephen23 2015년 9월 23일
편집: Stephen23 2015년 9월 23일
See my answer for a much simpler and 100x faster solution.
beatlewalrus
beatlewalrus 2015년 9월 24일
Thank you, but it turned out to be not applicable in my case. I had to be more precise: i'm using MathScript node in LabView. So for example C=setxor(A,B) works ok in MatLab but doesn't work in LabVIEW

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
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
Walter Roberson 2015년 9월 24일
Hee, cute.
Works fine for non-negative integers up to 65535 but not in general.
Stephen23
Stephen23 2015년 9월 24일
True. It is also more than 100x faster than the accepted solution :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by