gpt4 book ai didi

r - 如何用向量运算替换双 for 循环

转载 作者:行者123 更新时间:2023-12-01 13:52:35 25 4
gpt4 key购买 nike

我正在尝试创建自定义分布函数(基于半正弦函数)。现在我的原型(prototype)是一个双 for 循环。我四处查看了矢量化操作,但我仍在学习(对 R 非常陌生),因此不清楚如何清理它。最后我想要一个 NxN 矩阵来比较地球上各点之间的距离。这是我现在的测试数据:

coord
Latitude Longitude
1 16.34577 6.303545
2 12.49475 28.626396
3 27.79462 60.032495
4 44.42699 110.114216
5 -69.85409 87.946878

邪恶的双重 for 循环:

for (i in 1:dim(coord)[1]){
for(j in 1:dim(coord)[1]) # for each column {
mymat[i,j] = coord[i,1]*coord[j,2] # custom function for future
}
}

结果:

           X1         X2         X3        X4        X5
1 103.03629 467.9204 981.2773 1799.902 1437.559
2 78.76122 357.6796 750.0910 1375.850 1098.874
3 175.20461 795.6596 1668.5801 3060.582 2444.450
4 280.04755 1271.7847 2667.0632 4892.043 3907.215
5 -440.32840 -1999.6708 -4193.5152 -7691.928 -6143.449

当然,5个样本没问题。但是我有一个 100k 的列表。

搜索后确实看到了一个函数

custom.dist <- function(x, my.dist) {
mat <- sapply(x, function(x.1) sapply(x, function(x.2) my.dist(x.1, x.2)))
as.dist(mat)
}

但我不明白发生了什么,也无法让它工作,即使使用像 x*y

这样的虚拟函数也是如此

最佳答案

看起来您只想要外部产品。为此有一个函数 - 方便地命名为 outer。现在 outer 可以应用乘法以外的函数,但默认是乘法,所以我们不需要明确指定它。

> coord <- cbind(1:5, 2:6)
> coord
[,1] [,2]
[1,] 1 2
[2,] 2 3
[3,] 3 4
[4,] 4 5
[5,] 5 6
> outer(coord[,1], coord[,2])
[,1] [,2] [,3] [,4] [,5]
[1,] 2 3 4 5 6
[2,] 4 6 8 10 12
[3,] 6 9 12 15 18
[4,] 8 12 16 20 24
[5,] 10 15 20 25 30

请注意,这种方法也很容易推广到其他二元函数

> outer(coord[,1], coord[,2], FUN = paste0)
[,1] [,2] [,3] [,4] [,5]
[1,] "12" "13" "14" "15" "16"
[2,] "22" "23" "24" "25" "26"
[3,] "32" "33" "34" "35" "36"
[4,] "42" "43" "44" "45" "46"
[5,] "52" "53" "54" "55" "56"

关于r - 如何用向量运算替换双 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com