R:使用表达式作为下标的慢速矩阵索引

R:使用表达式作为下标的慢速矩阵索引,r,optimization,indexing,R,Optimization,Indexing,下面是我正在编写的代码的简化版本: # Randomly populated matrices as example/toy data. adata <- sample(1:25,2500,replace=T) xdim <- 50 ydim <- 50 dim(adata) <- c(xdim,ydim) # All possible categories cats <- 1:25 # Retrieve the coordinates of the memb

下面是我正在编写的代码的简化版本:

# Randomly populated matrices as example/toy data.

adata <- sample(1:25,2500,replace=T)
xdim <- 50
ydim <- 50
dim(adata) <- c(xdim,ydim)

# All possible categories
cats <- 1:25

# Retrieve the coordinates of the members of each class
# These subsets are used to calculate the minimum distances.

subA <- list(NULL)

for(i in 1:length(cats)){
  subA[[i]] <- which(adata==cats[i],arr.ind=T)
}


# Matrix containing Euclidean distances for each combination of dX and dY
eucdist <- matrix(nrow=xdim,ncol=ydim)
eucdist <- sqrt(((row(eucdist)-1)^2)+((col(eucdist)-1)^2))

# Create an array containing the x,y coordinates of each cell (in pixels). Used to  calculate distances

xycoor <- array(dim=c(xdim,ydim,2))
xycoor[,,1] <- row(xycoor[,,1])
xycoor[,,2] <- col(xycoor[,,2])


# For each cell, find the minimum distance to a cell belonging to each category

mindistA <- array(dim=c(xdim,ydim,length(cats)))

for(i in 1:length(cats)){
  # Calculates an array of the distance between each cell and the nearest cell of each class (of the same map)

  mindistA[,,i] <- apply(xycoor,1:2,function(x,y,z) min(eucdist[abs(sweep(subA[[i]],2,x,"-"))+1],na.rm=T))
}
#随机填充矩阵作为示例/玩具数据。

如果您需要改进工作代码方面的帮助,adata是正确的位置。我会在
R
软件包中查找欧几里德距离函数(使用,例如
sos::?
)。您还可以研究
parallel::mcapply
来执行多核操作。任何依赖于查找到类别中所有点的距离的方法都需要计算(或查找)所有点对之间的距离。在您的1000 x 2000示例中,您谈论的是4万亿次查找,这永远不会很快。有数据结构可以加速最近邻计算——我会检查k-d树和四叉树。@SvenHohenstein谢谢你的提示,很高兴知道,我不知道那一页。@CarlWitthoft有这么多核,我怀疑并行化作业会有很大的不同,但谢谢。