2017-05-04 48 views
-1

我有一个数字的Matlab矩阵,其中每个数字是每5分钟的一次测量。如何获得数字矩阵的索引?

如何查找由5个以上零分隔的每个块的开始和结束索引。它从右侧开始计数,并继续块直到找到大于5的零。即

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 

块是:

4 2 22 41 0 0 0 0 0 5 6 0 0 0 4 
1 0 0 4 0 1 2 

而且我想知道他们的指数。

我该怎么做?

+0

相关:http://stackoverflow.com/questions/3274043/finding-islands-of-zeros-in-a-sequence – excaza

回答

1

如果您有图像处理工具箱,则可以使用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