【问题标题】:How to create a Large Distance Matrix?如何创建大距离矩阵?
【发布时间】:2014-01-22 13:33:16
【问题描述】:

如何以适当的方式分配一个巨大的距离矩阵以避免“分配是 无法”错误。假设您有 100.000 个点随机分布在一些 空间。如何巧妙地创建一个矩阵或“dist”对象,它表示 DistMatrix 的一半。也许它应该是另一个对象,它将能够有效地分配大量的距离。

您可以从以下链接获取多边形对象: https://www.dropbox.com/sh/65c3rke0gi4d8pb/LAKJWhwm-l

# Load required packages
library(sp)
library(maptools)
library(maps)

# Load the polygonal object
x <- readShapePoly("vg250_gem.shp")

# Sample or Pick up a large Number of Points
# this command needs some minutes to be done. 
# "coord" is SpatialPoints Object
n <- 1e5
coord <- spsample(x, n, "random")
# Try to measure the distances by dist() 

DistMatrix <- dist(coord@coords)
Error: negative length vectors are not allowed

# Try to measure the distances by spDists()
DistMatrix <- spDists(coord)
Error: cannot allocate vector of size (some number) MB

# It seems that the problem lies on large matrix to be created. 

如何在 R 中解决大量“n”的问题。

【问题讨论】:

    标签: r distance spatial allocation euclidean-distance


    【解决方案1】:

    此时 R 无法分配随机数兆字节的 RAM。此时,您的计算机正在其他地方使用其所有内存,并且没有(一定数量)MBytes 可用于您的进程继续。此时您有几个解决方案。其中,获得更多 RAM 的机器,关闭程序,或者以较小的批次进行距离计算。尝试较小的 n;当它起作用时,只需重复该过程几次,直到您获得整个距离矩阵。

    【讨论】:

    • 说要小批量进行,您只是指使用 spDistsN1() 吗?请如果你能澄清你的想法,它会很有用。提前致谢。
    • 嗨。我不熟悉这种方法。但如果它对你有用。我实际上的意思是将您的距离矩阵划分为子矩阵并逐个求解每个子矩阵。每次都将结果保存在硬盘上。释放你的内存。
    • 你知道一些包可以加速简单的计算任务,比如 sum、sqrt 等等。我听说有一个包允许这样做,但我找不到。
    • 大内存包可能吗?