HW4_Code - test_diff_z =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
clear all;close all;clc sigma=sqrt(0.2); training(:,1:50)=[1;1]*ones(1,50)+sigma*randn(2,50); % Training samples from Class 1. Let us denote this class by '0' training(:,51:100)=[1.5;1.5]*ones(1,50)+sigma*randn(2,50); % Training samples from Class 2. Let us this denote this class by '1' testing(:,1:100)=[1;1]*ones(1,100)+sigma*randn(2,100); % Testing samples from Class 1 testing(:,101:200)=[1.5;1.5]*ones(1,100)+sigma*randn(2,100); % Testing samples from Class 2 testNN = zeros(200,1); test3NN = zeros(200,1); for t=1:length(testing) test = testing(:,t); % Pick the testing sample indexed t test_diff = test*ones(1,100) - training; % Find the difference vectors between the testing sample and training samples
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: test_diff_z = complex(test_diff(1,:),test_diff(2,:)); [training_sort,ind]=sort(abs(test_diff_z)); % Find the distance between the testing samples and training samples testNN(t) = double(ind(1) > 50); % assign 1 to the sample if the NN belongs to Class 2 (index > 50) test3NN(t) = (sum(double(ind(1:3) > 50)) > 1); % assign 1 to the sample if STRICTLY MORE than ONE of 3 NNs belong to Class 2 end P_E_NN = (sum(testNN(1:100))/100 + (1 - sum(testNN(101:200))/100))/2 % calculate the probability of error for NN rule P_E_3NN = (sum(test3NN(1:100))/100 + (1 - sum(test3NN(101:200))/100))/2 % calculate the probability of error for 3NN rule...
View Full Document

Ask a homework question - tutors are online