Matlab:如何找到范围? [英] Matlab: How to find ranges of ones?

查看:104
本文介绍了Matlab:如何找到范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个1和0的二进制向量.我想找到一个带有数字1的函数范围/孤岛. 例如:x = 0001111001111111000110 ... 我需要一个这样的答案:4-7(或4 5 6 7),10-16、20-21 ... 谢谢您的所有帮助!

I have a binary vector of 1s and 0s. I would like to find with a function ranges/islands of numbers 1. For example: x = 0001111001111111000110 ... I would need an answer like that: 4-7 (or 4 5 6 7), 10-16, 20-21 ... Thank you for all your help!

推荐答案

Aki解决方案的差异(未经过大量测试):

Variant of Aki's solution (not tested a lot):

x = [0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 0];
dx = diff([0, x, 0]);
start_pos = find(dx == 1);
end_pos = find(dx == -1) - 1;

这篇关于Matlab:如何找到范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆