How to generate a regular matrix of 48 samples with distances between them in Euclidian values in R?

33 Views Asked by At

I need to generate a distance matrix with the geographic (Euclidian) distances between 48 points sampled in a regular grid with exactly 120 centimeters between neighbouring points both vertically and horizontally:

Sampling matrix

Thus, the distance between sample 1K (cell A1) and both 1NP(cell A2) and 2WN(cell B1) is 120 cm, and 240 cm for 1K-1WNP and 1K-3W, and so on. Diagonally, the distances can be calculated using simple Pythagoras, so that the distance from 1K (A1) to 2WP (B2) is Squareroot(120^2 + 120^2)=169,7, and that from 1K(A1) to 2P(B3) is Squareroot(240^2 + 120^2)= 268,33, and so on.

This will make for a total matrix for distances between all points of 48x48. Here are the first two columns and 16 rows of how the matrix should look:

enter image description here

How does one do that in R? I have tried to define a set of data and use the dist() function, but that clearly does not work.

1

There are 1 best solutions below

1
Grzegorz Sapijaszko On BEST ANSWER

Use {sf} package: st_make_grid and st_distance functions like:

sfc = sf::st_sfc(sf::st_polygon(list(rbind(c(0,0), c(8*1.2,0), c(8*1.2,6*1.2), c(0, 6*1.2), c(0,0)))))

g <- sf::st_make_grid(sfc, n = c(7, 5), cellsize = 1.2, crs = "EPSG:2180", what = "corners")

plot(sfc)
plot(g, pch = 16, col = "blue", add = TRUE)


g |>
  sf::st_distance(g)
#> Units: [m]
#>            [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]      [,8]
#>  [1,]  0.000000 1.200000 2.400000 3.600000 4.800000 6.000000 7.200000  8.400000
#>  [2,]  1.200000 0.000000 1.200000 2.400000 3.600000 4.800000 6.000000  7.200000
#>  [3,]  2.400000 1.200000 0.000000 1.200000 2.400000 3.600000 4.800000  6.000000
#>  [4,]  3.600000 2.400000 1.200000 0.000000 1.200000 2.400000 3.600000  4.800000
#>  [5,]  4.800000 3.600000 2.400000 1.200000 0.000000 1.200000 2.400000  3.600000
#>  [6,]  6.000000 4.800000 3.600000 2.400000 1.200000 0.000000 1.200000  2.400000
#>  [7,]  7.200000 6.000000 4.800000 3.600000 2.400000 1.200000 0.000000  1.200000
#>  [8,]  8.400000 7.200000 6.000000 4.800000 3.600000 2.400000 1.200000  0.000000
#>  [9,]  1.200000 1.697056 2.683282 3.794733 4.947727 6.118823 7.299315  8.485281
#> [10,]  1.697056 1.200000 1.697056 2.683282 3.794733 4.947727 6.118823  7.299315
#> [11,]  2.683282 1.697056 1.200000 1.697056 2.683282 3.794733 4.947727  6.118823
#> [12,]  3.794733 2.683282 1.697056 1.200000 1.697056 2.683282 3.794733  4.947727
#> [13,]  4.947727 3.794733 2.683282 1.697056 1.200000 1.697056 2.683282  3.794733
#> [14,]  6.118823 4.947727 3.794733 2.683282 1.697056 1.200000 1.697056  2.683282
#> [15,]  7.299315 6.118823 4.947727 3.794733 2.683282 1.697056 1.200000  1.697056
#> [16,]  8.485281 7.299315 6.118823 4.947727 3.794733 2.683282 1.697056  1.200000
#> [17,]  2.400000 2.683282 3.394113 4.326662 5.366563 6.462198 7.589466  8.736132
#> [18,]  2.683282 2.400000 2.683282 3.394113 4.326662 5.366563 6.462198  7.589466
#> [19,]  3.394113 2.683282 2.400000 2.683282 3.394113 4.326662 5.366563  6.462198
#> [20,]  4.326662 3.394113 2.683282 2.400000 2.683282 3.394113 4.326662  5.366563
#> [21,]  5.366563 4.326662 3.394113 2.683282 2.400000 2.683282 3.394113  4.326662
#> [22,]  6.462198 5.366563 4.326662 3.394113 2.683282 2.400000 2.683282  3.394113
#> [23,]  7.589466 6.462198 5.366563 4.326662 3.394113 2.683282 2.400000  2.683282
#> [24,]  8.736132 7.589466 6.462198 5.366563 4.326662 3.394113 2.683282  2.400000
#> [25,]  3.600000 3.794733 4.326662 5.091169 6.000000 6.997142 8.049845  9.138928
#> [26,]  3.794733 3.600000 3.794733 4.326662 5.091169 6.000000 6.997142  8.049845
#> [27,]  4.326662 3.794733 3.600000 3.794733 4.326662 5.091169 6.000000  6.997142
#> [28,]  5.091169 4.326662 3.794733 3.600000 3.794733 4.326662 5.091169  6.000000
#> [29,]  6.000000 5.091169 4.326662 3.794733 3.600000 3.794733 4.326662  5.091169
#> [30,]  6.997142 6.000000 5.091169 4.326662 3.794733 3.600000 3.794733  4.326662
#> [31,]  8.049845 6.997142 6.000000 5.091169 4.326662 3.794733 3.600000  3.794733
#> [32,]  9.138928 8.049845 6.997142 6.000000 5.091169 4.326662 3.794733  3.600000
#> [33,]  4.800000 4.947727 5.366563 6.000000 6.788225 7.683749 8.653323  9.674709
#> [34,]  4.947727 4.800000 4.947727 5.366563 6.000000 6.788225 7.683749  8.653323
#> [35,]  5.366563 4.947727 4.800000 4.947727 5.366563 6.000000 6.788225  7.683749
#> [36,]  6.000000 5.366563 4.947727 4.800000 4.947727 5.366563 6.000000  6.788225
#> [37,]  6.788225 6.000000 5.366563 4.947727 4.800000 4.947727 5.366563  6.000000
#> [38,]  7.683749 6.788225 6.000000 5.366563 4.947727 4.800000 4.947727  5.366563
#> [39,]  8.653323 7.683749 6.788225 6.000000 5.366563 4.947727 4.800000  4.947727
#> [40,]  9.674709 8.653323 7.683749 6.788225 6.000000 5.366563 4.947727  4.800000
#> [41,]  6.000000 6.118823 6.462198 6.997142 7.683749 8.485281 9.372300 10.322790
#> [42,]  6.118823 6.000000 6.118823 6.462198 6.997142 7.683749 8.485281  9.372300
#> [43,]  6.462198 6.118823 6.000000 6.118823 6.462198 6.997142 7.683749  8.485281
#> [44,]  6.997142 6.462198 6.118823 6.000000 6.118823 6.462198 6.997142  7.683749
#> [45,]  7.683749 6.997142 6.462198 6.118823 6.000000 6.118823 6.462198  6.997142
#> [46,]  8.485281 7.683749 6.997142 6.462198 6.118823 6.000000 6.118823  6.462198
#> [47,]  9.372300 8.485281 7.683749 6.997142 6.462198 6.118823 6.000000  6.118823
#> [48,] 10.322790 9.372300 8.485281 7.683749 6.997142 6.462198 6.118823  6.000000
[...]
#>           [,41]    [,42]    [,43]    [,44]    [,45]    [,46]    [,47]     [,48]
#>  [1,]  6.000000 6.118823 6.462198 6.997142 7.683749 8.485281 9.372300 10.322790
#>  [2,]  6.118823 6.000000 6.118823 6.462198 6.997142 7.683749 8.485281  9.372300
#>  [3,]  6.462198 6.118823 6.000000 6.118823 6.462198 6.997142 7.683749  8.485281
#>  [4,]  6.997142 6.462198 6.118823 6.000000 6.118823 6.462198 6.997142  7.683749
#>  [5,]  7.683749 6.997142 6.462198 6.118823 6.000000 6.118823 6.462198  6.997142
#>  [6,]  8.485281 7.683749 6.997142 6.462198 6.118823 6.000000 6.118823  6.462198
#>  [7,]  9.372300 8.485281 7.683749 6.997142 6.462198 6.118823 6.000000  6.118823
#>  [8,] 10.322790 9.372300 8.485281 7.683749 6.997142 6.462198 6.118823  6.000000
#>  [9,]  4.800000 4.947727 5.366563 6.000000 6.788225 7.683749 8.653323  9.674709
#> [10,]  4.947727 4.800000 4.947727 5.366563 6.000000 6.788225 7.683749  8.653323
#> [11,]  5.366563 4.947727 4.800000 4.947727 5.366563 6.000000 6.788225  7.683749
#> [12,]  6.000000 5.366563 4.947727 4.800000 4.947727 5.366563 6.000000  6.788225
#> [13,]  6.788225 6.000000 5.366563 4.947727 4.800000 4.947727 5.366563  6.000000
#> [14,]  7.683749 6.788225 6.000000 5.366563 4.947727 4.800000 4.947727  5.366563
#> [15,]  8.653323 7.683749 6.788225 6.000000 5.366563 4.947727 4.800000  4.947727
#> [16,]  9.674709 8.653323 7.683749 6.788225 6.000000 5.366563 4.947727  4.800000
#> [17,]  3.600000 3.794733 4.326662 5.091169 6.000000 6.997142 8.049845  9.138928
#> [18,]  3.794733 3.600000 3.794733 4.326662 5.091169 6.000000 6.997142  8.049845
#> [19,]  4.326662 3.794733 3.600000 3.794733 4.326662 5.091169 6.000000  6.997142
#> [20,]  5.091169 4.326662 3.794733 3.600000 3.794733 4.326662 5.091169  6.000000
#> [21,]  6.000000 5.091169 4.326662 3.794733 3.600000 3.794733 4.326662  5.091169
#> [22,]  6.997142 6.000000 5.091169 4.326662 3.794733 3.600000 3.794733  4.326662
#> [23,]  8.049845 6.997142 6.000000 5.091169 4.326662 3.794733 3.600000  3.794733
#> [24,]  9.138928 8.049845 6.997142 6.000000 5.091169 4.326662 3.794733  3.600000
#> [25,]  2.400000 2.683282 3.394113 4.326662 5.366563 6.462198 7.589466  8.736132
#> [26,]  2.683282 2.400000 2.683282 3.394113 4.326662 5.366563 6.462198  7.589466
#> [27,]  3.394113 2.683282 2.400000 2.683282 3.394113 4.326662 5.366563  6.462198
#> [28,]  4.326662 3.394113 2.683282 2.400000 2.683282 3.394113 4.326662  5.366563
#> [29,]  5.366563 4.326662 3.394113 2.683282 2.400000 2.683282 3.394113  4.326662
#> [30,]  6.462198 5.366563 4.326662 3.394113 2.683282 2.400000 2.683282  3.394113
#> [31,]  7.589466 6.462198 5.366563 4.326662 3.394113 2.683282 2.400000  2.683282
#> [32,]  8.736132 7.589466 6.462198 5.366563 4.326662 3.394113 2.683282  2.400000
#> [33,]  1.200000 1.697056 2.683282 3.794733 4.947727 6.118823 7.299315  8.485281
#> [34,]  1.697056 1.200000 1.697056 2.683282 3.794733 4.947727 6.118823  7.299315
#> [35,]  2.683282 1.697056 1.200000 1.697056 2.683282 3.794733 4.947727  6.118823
#> [36,]  3.794733 2.683282 1.697056 1.200000 1.697056 2.683282 3.794733  4.947727
#> [37,]  4.947727 3.794733 2.683282 1.697056 1.200000 1.697056 2.683282  3.794733
#> [38,]  6.118823 4.947727 3.794733 2.683282 1.697056 1.200000 1.697056  2.683282
#> [39,]  7.299315 6.118823 4.947727 3.794733 2.683282 1.697056 1.200000  1.697056
#> [40,]  8.485281 7.299315 6.118823 4.947727 3.794733 2.683282 1.697056  1.200000
#> [41,]  0.000000 1.200000 2.400000 3.600000 4.800000 6.000000 7.200000  8.400000
#> [42,]  1.200000 0.000000 1.200000 2.400000 3.600000 4.800000 6.000000  7.200000
#> [43,]  2.400000 1.200000 0.000000 1.200000 2.400000 3.600000 4.800000  6.000000
#> [44,]  3.600000 2.400000 1.200000 0.000000 1.200000 2.400000 3.600000  4.800000
#> [45,]  4.800000 3.600000 2.400000 1.200000 0.000000 1.200000 2.400000  3.600000
#> [46,]  6.000000 4.800000 3.600000 2.400000 1.200000 0.000000 1.200000  2.400000
#> [47,]  7.200000 6.000000 4.800000 3.600000 2.400000 1.200000 0.000000  1.200000
#> [48,]  8.400000 7.200000 6.000000 4.800000 3.600000 2.400000 1.200000  0.000000

Created on 2024-02-24 with reprex v2.1.0

Please note the distances are in meters, you have to convert it to centimeters if required, for example:

g |>
  sf::st_distance(g) |>
  units::set_units(value = "centimeter")