MATLAB:确定一个值是否在向量中连续重复 N 次 [英] MATLAB: Identify if a value is repeated sequentially N times in a vector

查看:33
本文介绍了MATLAB:确定一个值是否在向量中连续重复 N 次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定一个值是否在向量中按顺序重复 N 次.我面临的挑战是它可以在向量内连续重复 N 次.目的是确定某行中某个值有多少次低于平均值.例如:

I am trying to identify if a value is repeated sequentially in a vector N times. The challenge I am facing is that it could be repeated sequentially N times several times within the vector. The purpose is to determine how many times in a row certain values fall above the mean value. For example:

>> return_deltas

return_deltas = 

      7.49828129642663
      11.5098198572327
      15.1776644881294
       11.256677995536
      6.22315734182976
      8.75582103474613
      21.0488849115947
       26.132605745393
      27.0507649089989
      ...

(例如,我只打印了几个值,但向量很大.)

(I only printed a few values for example but the vector is large.)

>> mean(return_deltas)

ans =

     10.50007490258002

>> sum(return_deltas > mean(return_deltas))

ans =

    50

因此 return_deltas 中有 50 个值的实例大于 return_deltas 的平均值.

So there are 50 instances of a value in return_deltas being greater than the mean of return_deltas.

我需要确定 return_deltas 中的值连续 3 次大于其平均值的次数.换句话说,如果 return_deltas 中的值连续 3 次大于其平均值,那就是一个实例.

I need to identify the number of times, sequentially, the value in return_deltas is greater than its mean 3 times in a row. In other words, if the values in return_deltas are greater than its mean 3 times in a row, that is one instance.

例如:

---------------------------------------------------------------------
| `return_delta` value | mean        | greater or less | sequence   |
|--------------------------------------------------------------------
|   7.49828129642663   |10.500074902 | LT              | 1          |
|  11.5098198572327    |10.500074902 | GT              | 1          |
|  15.1776644881294    |10.500074902 | GT              | 2          |
|   11.256677995536    |10.500074902 | GT              | 3 *        |
|  6.22315734182976    |10.500074902 | LT              | 1          |
|  8.75582103474613    |10.500074902 | LT              | 2          |
|  21.0488849115947    |10.500074902 | GT              | 1          |
|   26.132605745393    |10.500074902 | GT              | 2          |
|  27.0507649089989    |10.500074902 | GT              | 3 *        |
---------------------------------------------------------------------

星号代表连续 3 个成功的序列.这个集合的结果是两次,因为有两次值连续大于平均值 3 次.

The star represents a successful sequence of 3 in a row. The result of this set would be two because there were two occasions where the value was greater than the mean 3 times in a row.

我的想法是创建一个新的向量:

What I am thinking is to create a new vector:

>> a = return_deltas > mean(return_deltas)

当然包含 return_deltas 中的值大于均值的那些,并使用它来查找顺序中 return_deltas 中的值大于其均值的次数连续3次.我正在尝试使用内置函数(如果有,我还没有发现它)或至少避免循环来做到这一点.

that of course contains ones where values in return_deltas is greater than the mean and using it to find how many times sequentially, the value in return_deltas is greater than its mean 3 times in a row. I am attempting to do this with a built in function (if there is one, I have not discovered it) or at least avoiding loops.

对我的处理方式有什么想法吗?

Any thoughts on how I might approach?

推荐答案

通过一点点工作,这个代码段找到了每组数字的起始索引:

With a little work, this snippet finds the starting index of every run of numbers:

[0 find(diff(v) ~= 0)] + 1

一个例子:

>> v = [3 3 3 4 4 4 1 2 9 9 9 9 9];           # vector of integers
>> run_starts = [0 find(diff(v) ~= 0)] + 1    # may be better to diff(v) < EPSILON, for floating-point

run_starts =

     1     4     7     8     9

求每次运行的长度

>> run_lengths = [diff(run_starts), length(v) - run_starts(end) + 1]

这个变量可以很容易地查询哪些运行超过了特定数量

This variables then makes it easy to query which runs were above a certain number

>> find(run_lengths >= 4)

ans =

     5

>> find(run_lengths >= 2)

ans =

     1     2     5

这告诉我们,连续运行至少四个整数的唯一运行是运行 #5.
但是,有 3 次运行至少连续两个整数,特别是运行 #1、#2 和 #5.您可以从 run_starts 变量中引用每次运行的开始位置.

This tells us that the only run of at least four integers in a row was run #5.
However, there were three runs that were at least two integers in a row, specifically runs #1, #2, and #5. You can reference where each run starts from the run_starts variable.

这篇关于MATLAB:确定一个值是否在向量中连续重复 N 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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