如何使用索引向量从矩阵中提取行?

How do I use a vector of indices to extract rows from a matrix?

我有两个矩阵,M1 (6400x6) 和 M2 (315x3)

M2M1 的子集。我对 M2 做了一些数据插补,现在我想使用 M2 的行索引从 M1 中提取相应的行。我该如何处理?

谢谢

下面是一个可能有用的例子:

m1 <- matrix(runif(5000),ncol=50) #define a large matrix with random numbers
m2 <- m1[5:17,8:30] #create a subset starting from index [5,8]
#How to retrieve the information on the location of m2 within m1:
idx <- which(m1 %in% m2, arr.ind = TRUE)[1]
rownum <- idx %% nrow(m1)
colnum <- ceiling(idx / nrow(m1))
#> rownum
#[1] 5
#> colnum
#[1] 8

这意味着子集 m2 的初始点(可以说是左上角)位于较大的矩阵 m1 的第 5 行第 8 列。

希望对您有所帮助。但是,如果您执行了更复杂的修改并且子集不仅仅是大矩阵的简单矩形簇,并且条目以相同的方式排序,那么我相信您将需要提供更多信息来说明您为构建子矩阵以获得有用的答案。