计算抛硬币序列中的交替次数

发布于 2024-09-18 09:18:40 字数 185 浏览 14 评论 0原文

我有一个由 1 和 0 组成的序列,我想计算交替的次数,例如:

x <- rbinom(10, 1, 1/2)
> x
 [1] 0 0 1 1 1 1 1 0 1 0

因此我想计算(在 R 中)序列从 1 到 0 交替(或翻转)的次数。在上面的序列中,交替次数(用手数)为 4。

I have a sequence of ones and zeros and I would like to count the number of alternations, e.g:

x <- rbinom(10, 1, 1/2)
> x
 [1] 0 0 1 1 1 1 1 0 1 0

Thus I would like to count (in R) how many times the sequence alternates (or flips) from one to zero. In the above sequence, the number of alternations (counted by hand) is 4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠佳期 2024-09-25 09:18:40

您可以使用 diff() :

> x <- rbinom(10,1,1/2)

> x
 [1] 0 0 0 1 1 1 1 0 1 0

> sum(diff(x)!=0)
[1] 4

You can use diff() :

> x <- rbinom(10,1,1/2)

> x
 [1] 0 0 0 1 1 1 1 0 1 0

> sum(diff(x)!=0)
[1] 4
冷︶言冷语的世界 2024-09-25 09:18:40

rle 函数将计算向量中相同值的“游程”次数。因此,该向量的长度(负 1)为您提供了更改的数量:

> x
 [1] 0 0 0 1 1 1 1 0 1 0
> rle(x)
Run Length Encoding
  lengths: int [1:5] 3 4 1 1 1
  values : num [1:5] 0 1 0 1 0
> length(rle(x)$lengths)-1
[1] 4

可能比 diff() 方法更快或更慢,但如果您需要它们,它也会为您提供游程长度...

The rle function will count the number of 'runs' of the same value in a vector. Hence the length of this vector (minus 1) gives you the number of alterations:

> x
 [1] 0 0 0 1 1 1 1 0 1 0
> rle(x)
Run Length Encoding
  lengths: int [1:5] 3 4 1 1 1
  values : num [1:5] 0 1 0 1 0
> length(rle(x)$lengths)-1
[1] 4

Might be quicker or slower than the diff() method, but it also gives you the run lengths if you need them...

献世佛 2024-09-25 09:18:40

它绝对不会在优雅方面击败 diff,但另一种方式:

sum(x[-1] != head(x, n=-1))

在我的系统上,这似乎要快一点:

> x <- rbinom(1e6, 1, 0.5)
> system.time(replicate(100, sum(x[-1] != head(x, n=-1))))
   user  system elapsed 
  8.421   3.688  12.150 
> system.time(replicate(100, sum(diff(x) != 0)))
   user  system elapsed 
  9.431   4.525  14.248

似乎应该有一个很好的分析解决方案来分配中不相等的相邻元素的数量顺序。

It definitely doesn't beat diff in terms of elegance but another way:

sum(x[-1] != head(x, n=-1))

On my system, this seems to be a teeny bit faster:

> x <- rbinom(1e6, 1, 0.5)
> system.time(replicate(100, sum(x[-1] != head(x, n=-1))))
   user  system elapsed 
  8.421   3.688  12.150 
> system.time(replicate(100, sum(diff(x) != 0)))
   user  system elapsed 
  9.431   4.525  14.248

It seems like there ought to be a nice analytic solution for the distribution of the number of unequal adjacent elements in the sequence.

时光暖心i 2024-09-25 09:18:40

伪代码(序列是一个包含硬币翻转的数组):

variable count = 0
variable state = sequence[0]
iterate i from sequence[1] to sequence[n]
    if (state not equal sequence[i])
        state = 1 - state
        count++

计数应该是你的结果

Pseudocode (sequence is an array with your coin flips):

variable count = 0
variable state = sequence[0]
iterate i from sequence[1] to sequence[n]
    if (state not equal sequence[i])
        state = 1 - state
        count++

count should be your result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文