Advertisement
makispaiktis

ML - Minkowski Distance in n-D space

Oct 10th, 2022 (edited)
1,402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.61 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % Calculate the Minkowski distance for 2 spots
  5. n_List = [1, 2, 3, 10];
  6. r_list = 1 : 10;
  7. for k = 1 : length(n_List)
  8.  
  9.     n = n_List(k);
  10.     a = randn(n, 1);
  11.     b = randn(n, 1);
  12.     distances_r = zeros(1, length(r_list));
  13.     for i = 1 : length(r_list)
  14.         r = r_list(i);
  15.         SUM = 0;
  16.         for dim = 1 : n
  17.             SUM = SUM + abs(a(dim) - b(dim)) ^ r;
  18.         end
  19.         distance_r = (SUM)^(1/r);
  20.         distances_r(i) = distance_r;
  21.     end
  22.     plot(distances_r);
  23.     hold on
  24.    
  25. end
  26.  
  27. xlabel("r - Minkowski");
  28. ylabel("Distance(r)");
  29. legend("1D", "2D", "3D", "10D");
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement