Advertisement
makispaiktis

ML - Curse Dimensionality

Oct 10th, 2022 (edited)
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.88 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % Create random n-D spots
  5. % "x" will contain 500 spots with "n" dimensions-coordinates
  6. % Matrix will be Mxn, where M=500 spots with n coordinates
  7.  
  8. M = 500;
  9. N_max = 50;
  10. n_List = 1 : N_max;
  11. MAX_list = zeros(1, N_max);
  12. MIN_list = zeros(1, N_max);
  13. LOG_list = zeros(1, N_max);
  14.  
  15. for k = 1 : length(n_List)
  16.    
  17.     n = n_List(k);
  18.     spots = createSpots(M, n);
  19.     distances = createDistances(M, n, spots);
  20.     % distances(5, 7) is the distance between the 5-th and the 7-th
  21.     % spots/rows in the spots matrix
  22.     [MAX, MIN] = findMaxMin(M, n, distances);
  23.     LOG = log10((MAX - MIN) / MIN);
  24.     prettyPrint(M, n, MAX, MIN, LOG);
  25.     MAX_list(k) = MAX;
  26.     MIN_list(k) = MIN;
  27.     LOG_list(k) = LOG;
  28.    
  29. end
  30.  
  31. plot(n_List, LOG_list);
  32.  
  33.  
  34.  
  35. % AUXILIARY FUNCTIONS
  36. function spots = createSpots(M, n)
  37.     spots = zeros(M, n);
  38.     for i = 1 : M
  39.         for j = 1 : n
  40.             spots(i, j) = randn();
  41.         end
  42.     end
  43. end
  44.  
  45. function distances = createDistances(M, n, spots)
  46.     for elem1 = 1 : M
  47.         for elem2 = 1 : M
  48.             SUM = 0;
  49.             for dim = 1 : n
  50.                 SUM = SUM + (spots(elem1, dim) - spots(elem2, dim))^2;
  51.             end
  52.             distances(elem1, elem2) = sqrt(SUM);
  53.         end
  54.     end
  55. end
  56.  
  57. function [MAX, MIN] = findMaxMin(M, n, distances)
  58.     MAX = max(max(distances));
  59.     MIN = Inf;
  60.     for i = 1 : M
  61.         for j = 1 : M
  62.             value = distances(i, j);
  63.             if value > 0 && value < MIN
  64.                 MIN = value;
  65.             end
  66.         end
  67.     end
  68. end
  69.  
  70. function prettyPrint(M, n, MAX, MIN, LOG)
  71.     display('*****************************************');
  72.     disp("M = " + num2str(M) + " " + num2str(n)+ "-D spots");
  73.     disp("Max = " + num2str(MAX)+ ", MIN = " + num2str(MIN) + ", LOG = " + num2str(LOG));
  74.     display('*****************************************');
  75.     display(' ');
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement