如何获得数字矩阵的索引?

时间:2017-05-04 18:34:28

标签: matlab

我有一个数字的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

我想知道他们的指数。

我该怎么做?

1 个答案:

答案 0 :(得分: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