How to find the distance among matrix values in R?


Finding the distance among matrix values means that we want to find the distance matrix and it can be directly found by using dist function with the matrix name. For example, suppose we have a matrix of size 5x5 named as M then the distance matrix can be calculated as dist(M).

Example1

M1<−matrix(1:36,ncol=6)
M1

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 7 13 19 25 31
[2,] 2 8 14 20 26 32
[3,] 3 9 15 21 27 33
[4,] 4 10 16 22 28 34
[5,] 5 11 17 23 29 35
[6,] 6 12 18 24 30 36
dist(M1)
1 2 3 4 5
2 2.449490
3 4.898979 2.449490
4 7.348469 4.898979 2.449490
5 9.797959 7.348469 4.898979 2.449490
6 12.247449 9.797959 7.348469 4.898979 2.449490

Example2

M2<−matrix(rnorm(36,2,0.98),ncol=6)
M2

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.30918719 2.10924445 2.290298 2.777059 2.4907988 1.687226
[2,] 3.38356559 -0.06665392 1.931708 2.293665 3.3287502 2.599893
[3,] 2.34493075 2.45465751 2.213626 2.902613 0.3604137 2.228618
[4,] 1.31512331 2.88142144 1.311969 4.426748 1.2890785 1.094872
[5,] -0.08505025 0.62849704 1.833984 2.217687 2.9674727 1.656955
[6,] 2.69655356 3.74480867 3.802174 2.370346 1.8752234 3.178781
dist(M2)
1 2 3 4 5
2 4.010451
3 3.019412 4.103040
4 2.661162 4.932451 2.554683
5 1.759912 3.680965 4.120252 3.914275
6 3.664620 4.577293 2.791483 4.215441 4.985454

Example3

M3<−matrix(rpois(64,10),ncol=8)
M3

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 10 14 11 8 10 17 8 10
[2,] 8 9 7 11 15 13 16 12
[3,] 10 10 10 20 11 9 5 6
[4,] 10 11 4 9 10 6 7 13
[5,] 12 9 8 10 5 8 9 4
[6,] 12 10 15 10 11 6 9 16
[7,] 8 6 9 10 8 9 9 7
[8,] 17 10 12 6 9 9 9 11
dist(M3)
1 2 3 4 5 6 7
2 12.767145
3 15.842980 16.852300
4 13.784049 13.304135 14.866069
5 13.601471 16.000000 12.884099 11.789826
6 14.106736 14.560220 15.937377 11.874342 15.297059
7 12.409674 12.369317 12.124356 10.392305 6.708204 12.922848
8 11.704700 15.297059 17.146428 11.874342 11.135529 9.380832 11.789826

Example4

M4<−matrix(sample(0:5,49,replace=TRUE),ncol=7)
M4

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0 1 4 2 2 2 2
[2,] 4 4 2 4 0 1 2
[3,] 3 0 5 5 2 5 5
[4,] 3 2 3 0 5 3 1
[5,] 2 2 3 4 5 5 3
[6,] 1 3 1 1 5 5 4
[7,] 1 5 4 4 2 2 1
dist(M4)
1 2 3 4 5 6
2 6.164414
3 6.164414 7.483315
4 5.099020 7.211103 7.874008
5 5.385165 7.141428 4.795832 5.000000
6 6.082763 8.062258 7.416198 4.795832 4.000000
7 4.690416 4.472136 7.483315 6.324555 5.744563 7.000000

Example5

M5<−matrix(sample(101:105,64,replace=TRUE),ncol=8)
M5

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 105 104 103 103 104 105 103 105
[2,] 101 101 103 103 104 104 101 105
[3,] 102 103 101 103 101 101 102 104
[4,] 103 103 102 101 103 103 104 104
[5,] 102 102 101 104 102 101 104 101
[6,] 101 105 104 101 103 102 104 104
[7,] 103 105 101 104 104 104 101 101
[8,] 105 105 105 103 104 105 101 101
dist(M5)
1 2 3 4 5 6 7
2 5.477226
3 6.403124 5.385165
4 4.123106 5.000000 4.242641
5 7.416198 6.708204 4.000000 5.099020
6 5.830952 6.000000 5.196152 3.605551 6.244998
7 5.567764 6.403124 5.830952 5.830952 5.656854 6.708204
8 5.000000 7.280110 8.000000 6.633250 8.000000 7.000000 4.690416

Updated on: 07-Nov-2020

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements