“滑动窗口"/元素上的分区 [英] "sliding window"/partition over elements

查看:23
本文介绍了“滑动窗口"/元素上的分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定序列1 2 3 4,我想生成一个矩阵对

Given the sequence 1 2 3 4, I would like to generate a matrix of pairs

1 2
2 3
3 4

用作另一个矩阵的索引.实现这一目标的最快方法是什么?

to use as indexes for another matrix. What would be the fastest way to achieve this?

推荐答案

您可以使用 embed(),反转输出中的列.

You could use embed(), reversing the columns on the output.

embed(1:4, 2)[, 2:1]
#      [,1] [,2]
# [1,]    1    2
# [2,]    2    3
# [3,]    3    4

这篇关于“滑动窗口"/元素上的分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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