mashlukashova

Untitled

Apr 23rd, 2021
639
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clear all;
  2. close all;
  3. clc
  4. X_nach = load('/Users/marialukasova/Downloads/CD_ИАД_2019-3/ЛР_5/Data_Lab5/data4.txt');
  5. x = X_nach(:, 1);
  6. y = X_nach(:, 2);
  7. %S = size(X_nach);
  8.  
  9. figure;
  10. plot (x, y, '*');
  11.  
  12. k = 2;
  13. C = zeros(k,2);
  14.  
  15. for i = 1 : k
  16.     C(i, :) = X_nach(i , :);
  17. end
  18.  
  19.  
  20. U = zeros(length(X_nach) , 2);
  21. eps = 1e-6;
  22. Q = 1000;
  23. Q_minus = 0;
  24.  
  25. R = zeros(1,k);
  26. for i = 1 : length(X_nach)  
  27.         for n = 1 : k
  28.         R(n) = pdist([X_nach(i , :) ; C(n , :)],'euclidean'); %расстояния
  29.  
  30.         end
  31.  
  32.     for n = 1 : k
  33.         if R(n) == min(R)
  34.             U(i,1) = n;
  35.             U(i,2) = R(n);
  36.         end
  37.     end
  38. end
  39.  
  40. %temp = 0;
  41.  
  42. while abs(Q - Q_minus)>eps
  43.  %   temp = temp+ 1
  44.     Q = Q_minus;
  45.    
  46.     C = zeros(k,2);
  47.     for l = 1 : k
  48.         n = 0;
  49.         for j = 1 : length(U)
  50.             if (U(j,1)==l)
  51.                 n = n+1;
  52.                 C(l, :) = C(l, :) + X_nach(j , :);
  53.                %disp(C);
  54.             end
  55.         end
  56.         C(l, :) = C(l, :)./n; %среденее арифм коорд = центр
  57.     end
  58.    
  59.     P = zeros(1,k);
  60.     for i = 1 : length(X_nach)
  61.         for n = 1 : k
  62.         P(n) = pdist([X_nach(i , :) ; C(n , :)],'euclidean');
  63.         end
  64.  
  65.         for n = 1 : k
  66.             if P(n) == min(P)
  67.                 U(i,1) = n;
  68.                 U(i,2) = P(n);
  69.             end
  70.         end
  71.     end
  72.    
  73.     u = zeros(1,k);
  74.     for l = 1 : k  
  75.         for j = 1 : length(U)
  76.             if (U(j,1)==l)
  77.                 u(l) = u(l)+U(j,2);
  78.             end
  79.         end
  80.     end
  81.     Q_minus = sum(u);
  82.  
  83. end
  84.  
  85. figure
  86. gscatter(X_nach(:,1), X_nach(:,2), U(:,1))
  87. hold on
  88. scatter(C(:,1),C(:,2),'X', 'k')    
  89.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×