使用R中的重叠列从原始矩阵创建更大的矩阵

时间:2014-05-27 09:26:42

标签: r overlap

想象一下,我们有一个5 * 5(25个元素)的矩阵

m<-matrix(1:25,5,5)
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

我想从矩阵“m”生成不同尺寸的大矩阵 例如5 * 8,现在我的第二列新矩阵必须与第一列重叠,依此类推。

如此大的尺寸(例如5 * 8)矩阵就像:(这只是一个例子而且不确定移位量是否正确)

      [,1] [,2] [,3] [,4] ……………………[,8]
[1,]    1    4   7    10 …………………… 19
[2,]    2    5   8    11 …………………… 20  
[3,]    3    6   9    12 …………………… 21  
[4,]    4    7   10   13 …………………… 22 
[5,]    5    8   11   14 …………………… 23   

实际上在每一列中我们都会转回到最后一列的某些元素,以防止到达原始矩阵的最后一个元素并产生NA值。 请有人知道如何创建这么大的矩阵吗?

对我来说最困难的部分是计算关于较大矩阵大小的SHIFT值。较大的矩阵必须覆盖原始矩阵的几乎所有元素。 (可以错过最后的元素) 感谢

2 个答案:

答案 0 :(得分:0)

我不确定你想要什么,但这可能会有所帮助。

rows <- 5
cols <- 8
overlap <- 1
matrix(rep(seq(1,cols)*(rows-overlap),each=rows)+seq(1,rows)-(rows-overlap),nrow=rows)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    5    9   13   17   21   25   29
[2,]    2    6   10   14   18   22   26   30
[3,]    3    7   11   15   19   23   27   31
[4,]    4    8   12   16   20   24   28   32
[5,]    5    9   13   17   21   25   29   33
overlap <- 2
matrix(rep(seq(1,cols)*(rows-overlap),each=rows)+seq(1,rows)-(rows-overlap),nrow=rows)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    4    7   10   13   16   19   22
[2,]    2    5    8   11   14   17   20   23
[3,]    3    6    9   12   15   18   21   24
[4,]    4    7   10   13   16   19   22   25
[5,]    5    8   11   14   17   20   23   26
overlap <- 3
matrix(rep(seq(1,cols)*(rows-overlap),each=rows)+seq(1,rows)-(rows-overlap),nrow=rows)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    3    5    7    9   11   13   15
[2,]    2    4    6    8   10   12   14   16
[3,]    3    5    7    9   11   13   15   17
[4,]    4    6    8   10   12   14   16   18
[5,]    5    7    9   11   13   15   17   19

答案 1 :(得分:0)

最大覆盖效率是:(最佳重叠值)

> overlap<-ceiling(rows*(1-length(dna1)/(cols*rows)))+round(rows/cols)