카테고리 없음

2 행렬에 대한 함수 적용

cj92 2021. 6. 10. 12:40

최근에 특정 지점과 위도에 대한 100by 100 행렬, 경도에 대한 100 by 100행렬에 대해 거리를 구하여

 

100 by 100의 행렬로 만들어야 하는 상황이 발생 했다.

 

처음에는 자료를 matrix를 펼쳐서 연산을 하려 했으나, matrix 구조를 유지한 채 3차원끼리의 연산을 해보고 싶었다. 

 

 

적용방식은 의외로 간단했는데 다음과 같이 적용을 하니 3차원에 대해 거리가 주어졌었다.

 

 

 

library(geosphere)
library(abind)
station=data.frame(lon=126,lat=37)
df=abind(lon,lat,along=3)
o1=apply(df,c(1,2),function(x){distGeo(c(station$lon,station$lat),c(x))})

 

https://stackoverflow.com/questions/3197330/using-apply-on-a-multidimensional-array-in-r

 

Using apply on a multidimensional array in R

I am wondering how to use apply on a multidimensional array. I have something like the following: A <- array(0, c(2, 2, 5)) for(i in 1:5) { A[, , i] <- matrix(rnorm(4), 2, 2) } I would l...

stackoverflow.com