简体   繁体   English

从更大的矩阵创建许多新矩阵

[英]Create many new matrices from a larger matrix

I am relatively new to R, though I have done a good amount of simple R programming. 尽管我做了很多简单的R编程工作,但我对R还是比较陌生。 I think this should be an easy question but I can't seem to figure it out. 我认为这应该是一个简单的问题,但我似乎无法弄清楚。

Updated: 更新:

The situation is that I need to fragment my data for regression analysis due to memory constraints on my computer. 情况是由于计算机内存不足,我需要对数据进行分段以进行回归分析。 I essentially have three matrices that are pertinent call them X (nxk), y (nx 1), and Om ( nxn) I need to break these three matrices up by rows multiply them together in various ways and then add up the results. 我基本上有三个相关的矩阵,分别称为X(nxk),y(nx 1)和Om(nxn)。我需要按行将这三个矩阵分解,然后以各种方式将它们相乘,然后将结果相加。 because of the error structure of Om some groups have to be rows of 3, others rows of 2 and others rows of 1. For a group of 3 we would have: 由于Om的错误结构,某些组必须是3行,其他行2行,其他行1行。对于3组,我们将有:

Xi (3 xk), yi (3 x 1) and Om3 (3 x 3) Xi(3 xk),yi(3 x 1)和Om3(3 x 3)

I have Om1, Om2, and Om3 already build in R 我已经在R中建立了Om1,Om2和Om3

Om1<-matrix(2)

vec1<-c(2,-1)
vec2<-c(-1,2)
Om2<-rbind(vec1,vec2)

vec3<-c(2,-1,0)
vec4<-c(-1,2,-1)
vec5<-c(0,-1,2)
Om3<-rbind(vec3,vec4, vec5)

Now the Question is how can I break up X, and y so that I can match the rows of X, y and Om. 现在的问题是如何分解X和y,以便匹配X,y和Om的行。 I was thinking I would need a loop but cannot get it to work something like: 我当时以为我需要一个循环,但无法使其工作:

for(i in 1:280){
  assign(paste("xh", i, sep = ""), i)    
}
for(i in 1:145){
  xhi<- X[i,] 
}
for(i in 146:195){
  xhi<-X[ seq( from=i , length=2) , ]
}

for(i in 196:280){
  xhi<-X[ seq( from=(i+49) , length=3) , ]
}

Where the first 145 xi 's correspond to Om1, the next 50 xi's correspond to Om2...I was thinking I would need a way to index them that was the same as eventually i need to sum up a product of xi, yi and Omi across i's 前145个xi对应于Om1,下一个50 xi对应于Om2 ...我在想我需要一种索引它们的方法,就像最终我需要对xi,yi和a的乘积求和一样跨我的Omi

Sorry for the long post trying to be thorough, any advice would be greatly appreciated 很抱歉长篇文章要详尽,任何建议将不胜感激

Assume this matrix is named mtx , you want 10 such matrices of increasing row count, and that it has at least 55 rows, since the sum of the lengths that grow by one with each iteration is n(n+1)/2: 假设此矩阵名为mtx ,则您需要10个这样的增加行数的矩阵,并且它至少具有55行,因为每次迭代的长度之和为n(n + 1)/ 2:

mlist <- list()
for (x in 1:10) mlist[[x]] <-mtx[ seq( from=x*(x-1)/2+1 , length=x) , ]

If the desire is as Carl suggests it would be: 如果欲望是卡尔所暗示的,那就是:

mlist <- list()
for (x in 1:10) mlist[[x]] <-mtx[ seq( from=1 , length=x) , ]

Or: 要么:

mlist <- list()
for (x in 1:10) mlist[[x]] <-mtx[ seq( from=x , length=2) , ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM