[英]Nested loops in R
I've got a problem that I'm sure has a simple solution but I can't figure it out. 我有一个问题,我确定有一个简单的解决方案,但我不知道。
Background: I've got several matrices which I have brought in to R, initially as a list but now they are in a "unified" matrix (this format is required for later processing). 背景:我有几个矩阵,最初是作为列表引入到R中的,但现在它们处于“统一”矩阵中(此格式对于以后的处理是必需的)。 I need to perform a calculation on every row (subtracting a vector);
我需要对每一行进行计算(减去向量); I need to substract a vector of the same length.
我需要减去一个相同长度的向量。 The specific vector for substraction is held in a list of 32 unique vectors.
用于减法的特定向量保存在32个唯一向量的列表中。
Within the unified matrix are 150 data frames which have 32 rows (giving a total of 4800 rows in the unified matrix). 统一矩阵内有150个数据帧,其中包含32行(统一矩阵中总共有4800行)。 The subtraction vector is 1021 in length, which corresponds to the same amount of columns in the unified matrix.
减矢量的长度为1021,对应于统一矩阵中的相同列数。
dat.y <- rep(1:32, times=150)
rows <- 32
d.list is a list of 32 1D vectors each 1021 in length d.list是32个1D向量的列表,每个向量的长度为1021
mattiff is a matrix 4800x1021 which was previously 150 tiff images of dimensions 32x1021 mattiff是矩阵4800x1021,以前是尺寸为32x1021的150张tiff图像
for (i in 1:length(dat.y)
{
for (j in 1:length(rows))
{
mattiff2<-mattiff[i, ] - d.list[[j]]
}
}
Essentially, I want to repeat a loop of the substraction of 32 rows 150 times across the unified matrix. 本质上,我想在整个统一矩阵上重复执行32次相减150次的循环。
Reproducable example: 可重现的示例:
mm <- matrix(100, 128, 1021)
list_sub<-list()
rows<-seq(1, 32, 1)
dat.len<-seq(1, 128, 1)
dat.y<-rep(1:32, times=4)
## random data to substract from mm
for (i in 1:32)
{
list_sub[[i]]<-runif(n = 1021, min = 45, max = 80)
}
## I want this to substract list_sub[[i]] from mm[i,]
## essentially making the loop of 32 rows across all 128
## rows of mm. I.e substracting from mm rows 1...32 with list_sub
## then substracting from mm rows 33...64 with list_sub
## then subtracting from mm rows 65...96 with list_sub
## then subtracting from mm rows 97...128 with list_sub
## I would imagine an approach similar to this would give me the result I want
## to give mm_2: a matrix with the same dimensions of mm
for (i in 1:length(dat.y))
{
for (j in 1:length(rows))
{
mm_2 <- mm[i,] - list_sub[[j]]
}
}
So you have 150 matrices of order 32x1021 unified (rbinded) in a matrix mattiff which is 4800x1021 and you want to subtract to rows 1,33,65,... the elements of d.list[[1]], to rows 2,34,66,... the elemnts of d.list[[2]] and so on.. 因此,在矩阵mattiff中,有150个32x1021阶的矩阵(统一),为4800x1021,并且您想将d.list [[1]]的元素减去到第1,33,65行... ,34,66,... d.list [[2]]的元素,依此类推。
I am assuming 我假设
mattiff2<-mattiff[i, ] - d.list[[j]]
is wrong (as is the reproducible example) and what you want is mattiff2 to be a matrix 4800x1021 with the resulting subtractions. 是错误的(如可重现的示例),您想要的是mattiff2成为矩阵4800x1021,并减去结果。
Here is a way: 这是一种方法:
# arrange the elements of d.list as a matrix 1021x32
d.array <- simplify2array(d.list)
# repeat d.array 150 times for a matrix 1021x4800
sub <- matrix(d.array,1021,4800)
# subtract elementwise
mattiff2 <- mattiff - t(sub)
Substitute 4800 with length(dat.y) if needed. 如果需要,将4800替换为length(dat.y)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.