本文介绍了从矩阵中提取一系列连续的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为简化起见,我有一个大矩阵:
I have a large matrix, say for simplification:
> mat = matrix(c(1:50), ncol = 5)
> mat
[,1] [,2] [,3] [,4] [,5]
[1,] 1 11 21 31 41
[2,] 2 12 22 32 42
[3,] 3 13 23 33 43
[4,] 4 14 24 34 44
[5,] 5 15 25 35 45
[6,] 6 16 26 36 46
[7,] 7 17 27 37 47
[8,] 8 18 28 38 48
[9,] 9 19 29 39 49
[10,] 10 20 30 40 50
我想从每一列中连续提取所有可能的垂直向量,这些向量由3个元素组成,例如(1,2,3),(2,3,4),...,(11,12,13),(12,13,14),...并生成由以下项组成的3-by-X矩阵所有这些向量.看起来像这样:
I want to extract successively all possible vertical vectors composed of 3 elements from each column, e.g. (1,2,3), (2,3,4),...,(11,12,13), (12,13,14),... and generate a 3-by-X matrix composed of all these vectors.Something that would look like this:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 4
[3,] 3 4 5
etc. . . .
[5,] 11 12 13
[6,] 12 13 14
[7,] 13 14 15
etc. .. .. ..
[9,] 21 22 23
[10,] 22 23 24
我尝试使用lapply和cbind函数,以及更通用的for循环,但是它显示维数错误".感谢您的帮助!
I have tried using the lapply and cbind functions, as well as a more generic for loop, but it shows "incorrect number of dimensions". Thanks for your help!
推荐答案
我们可以使用base R
do.call(rbind, lapply(as.data.frame(mat), embed, 3))[,3:1]
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 2 3 4
# [3,] 3 4 5
# [4,] 4 5 6
# [5,] 5 6 7
# [6,] 6 7 8
# [7,] 7 8 9
# [8,] 8 9 10
# [9,] 11 12 13
#[10,] 12 13 14
#[11,] 13 14 15
#[12,] 14 15 16
#[13,] 15 16 17
#[14,] 16 17 18
#[15,] 17 18 19
#[16,] 18 19 20
#[17,] 21 22 23
#[18,] 22 23 24
#[19,] 23 24 25
#[20,] 24 25 26
#[21,] 25 26 27
#[22,] 26 27 28
#[23,] 27 28 29
#[24,] 28 29 30
#[25,] 31 32 33
#[26,] 32 33 34
#[27,] 33 34 35
#[28,] 34 35 36
#[29,] 35 36 37
#[30,] 36 37 38
#[31,] 37 38 39
#[32,] 38 39 40
#[33,] 41 42 43
#[34,] 42 43 44
#[35,] 43 44 45
#[36,] 44 45 46
#[37,] 45 46 47
#[38,] 46 47 48
#[39,] 47 48 49
#[40,] 48 49 50
这篇关于从矩阵中提取一系列连续的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!