连续数字系列(不同长度) [英] Series of consecutive numbers (different lengths)

查看:92
本文介绍了连续数字系列(不同长度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人向我展示了一种简便的方法,我将不胜感激.假设我在MATLAB中有一个向量,例如

I would appreciate if someone showed me an easy way to do this. Let's say I have a vector in MATLAB like

d = [3 2 4 2 2 2 3 5 1 1 2 1 2 2 2 2 2 9 2]

我想找到连续数字二"的序列以及这些序列的长度.

I want to find the series of consecutive number "twos" and the lengths of those series.

通过x=find(d==2)可以容易地找到数字二.但是我想要的是得到一个包含所有连续二号序列的长度的向量,这意味着在这种情况下,我的结果将是这样的向量:

Number twos can easily be found by x=find(d==2). But what I want is to get a vector which contains the lengths of all series of consecutive number twos, which means that my result in this case would be a vector like this:

[1 3 1 5 1].

有人可以帮助我吗?

推荐答案

这似乎可行:

q = diff([0 d 0] == 2);
v = find(q == -1) - find(q == 1);

给予

v =

   1   3   1   5   1

对我来说

这篇关于连续数字系列(不同长度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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