Demo entry 5930651

Matlab

   

Submitted by anonymous on Sep 05, 2016 at 18:15
Language: Matlab. Code size: 684 Bytes.

w1 = [2 7; 8 1 ;7 5 ;6 3 ;7 8 ;5 9 ;4 5];
w2 = [4 2; -1 -1; 1 3; 3 -2; 5 3.25; 2 4; 7 1 ];
X = [w1; w2];

Y = ones(size(X, 1), size(X, 2) + 1);
Y(:, 2:3) = X;
Y(1:size(w1, 1), :) = -Y(1:size(w1, 1), :);
Y = Y.';

Y1 = Y(2, :).';
Y2 = Y(3, :).';

scatter(w1(:, 1), w1(:, 2), 'o');
hold on;
scatter(w2(:, 1), w2(:, 2), 'x');

w = -1 + (rand(size(Y, 1), 1) * 2);

n = size(Y, 2);
k = 1;
h = w.' * Y;
c = (h < 0);

while sum(c) > 0
    y = Y(:, k);
    g = w.' * y;
    if g < 0
        w = w + Y(:, k);
    end
    
    h = w.' * Y;
    
    c = (h < 0);
    
    k = k + 1;
    if k == n + 1
        k = 1;
    end
end
w

f = @(x) (w(2) * x + w(1)) / -w(3);
hold on;
fplot(f, [-10 10]);

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).