如果您有图像处理工具箱,则可以使用bwareaopen
来实现此目的。
A = [1 0 0 4 0 1 2 0 0 0 0 0 0 4 2 22 41 0 0 0 0 0 5 6 0 0 0 4]; %Given array
tmp=~bwareaopen(~A, 6); %Logical array of the blocks separated by greater than 5 zeros
tmp = diff([0, tmp, 0]); %Padded with zeroes for the first & last indices respectively
startInd = find(tmp == 1); %starting indices of the blocks
endInd = find(tmp == -1) - 1; %ending indices of the blocks
对于给定的阵列中,它给出:
>> startInd
startInd = %1st block starts from the 1st index, 2nd block starts from the 14th index
1 14
>> endInd
endInd = %1st block ends at the 7th index, 2nd block ends at the 28th index
7 28
相关:http://stackoverflow.com/questions/3274043/finding-islands-of-zeros-in-a-sequence – excaza