mashlukashova

Untitled

May 9th, 2021
630
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clear all;
  2. close all;
  3. clc
  4. X = load('/Users/marialukasova/Downloads/CD_ИАД_2019-3/ЛР_6/Data_Lab6/data4.txt');
  5.  
  6. figure;
  7. plot(X(:,1), X(:,2), '*');
  8.  
  9. numNeuron = 2;
  10. C = zeros(numNeuron,2);
  11. mnX = min(X(:,1));
  12. mxX = max(X(:,1));
  13. mnY = min(X(:,2));
  14. mxY = max(X(:,2));
  15.  
  16. C(1,1) = mnX + (mxX-mnX).*rand();
  17. C(2,1) = mnX + (mxX-mnX).*rand();
  18. C(1,2) = mnY + (mxY-mnY).*rand();
  19. C(2,2) = mnY + (mxY-mnY).*rand();
  20.  
  21. k_max = 1000;
  22. h=0.01;
  23.  
  24.  
  25. R = zeros(1,numNeuron);
  26.  
  27. for k = 1:k_max
  28.    
  29.     num = ceil(1+(length(X)-1).*rand());
  30.    
  31.     for n = 1 : numNeuron
  32.          R(n) = pdist([X(num , :) ; C(n , :)],'euclidean');
  33.     end
  34.    
  35.     nC = C;
  36.    
  37.     for n = 1 : numNeuron
  38.         if R(n) == min(R)
  39.           nC(n,1) = C(n,1)+ h*(X(num,1)-C(n,1));
  40.           nC(n,2) = C(n,2)+ h*(X(num,2)-C(n,2));
  41.         end
  42.     end
  43.  
  44.     C = nC;
  45.    
  46. end
  47.  
  48. U = zeros(length(X));
  49. for i = 1 : length(X)
  50.         for n = 1 : numNeuron
  51.         P(n) = pdist([X(i , :) ; C(n , :)],'euclidean');
  52.         end
  53.  
  54.         for n = 1 : numNeuron
  55.             if P(n) == min(P)
  56.                 U(i) = n;
  57.                
  58.             end
  59.         end
  60. end
  61.  
  62.  
  63.  
  64.  
  65. figure
  66. scatter(C(:,1),C(:,2),'*')
  67. hold on
  68. gscatter(X(:,1), X(:,2), U(:,1))
RAW Paste Data