我想编写一个简短的Matlab函数,用于在这样的时间序列中找到序列值:
例:a=0 0 0 1 0 0 1 1 1 0 0;
My_expected_result =3;(当数字1发生时,3次序列)
谢谢。
发布于 2018-10-27 00:17:10
下面是一个简单的regexp-based解决方案,用于查找运行次数:
regexp
result = numel(regexp(char(a+'0'), '1+'));
您还可以使用strfind,它适用于数值数组(尽管这还没有文档记录):
strfind
result = numel(strfind([0 a], [1 0]));
或者仅仅是diff
diff
result = sum(diff([a 0])<0);
如果您有图像处理工具箱,bwlabel也可以用于该工作:
bwlabel
result = max(bwlabel(a));
或者(这要感谢@rahnema1 ):
[~, result] = bwlabel(a);
https://stackoverflow.com/questions/53017692
相似问题