knnclassify( )


knnclassify( ) function in Matlab


Syntax:           class = knnclassify(sample, training, group)


Description:  class = knnclassify(sample, training, group) classifies the rows of the data matrix of sample into groups, based on the grouping of the rows of training. sample and training must be matrices with same number of columns. group is a vector whose distinct value define the grouping of the rows in training. Each row of training belongs to the group whose values is the corresponding  entry of group. knnclassify assigns each row of sample to the group  for the closest row of training. group can be numeric vector, a string array, or a cell of strings. training and group must have the same number of rows. knnclassify treats NaNs or empty strings in group as missing values, and ignores the corresponding rows of training. class indicates which group each row of sample has been assigned to, and is of the same type as group.









Code :


% Matlab Function - knnclassify
% Syntax : 
% class = knnclassify(sample, training, group)

% Sample Matrix

A = [50, 60;
    7, 2;
    13, 12;
    100, 200];

% Training Matrix

B = [1, 0;
    200, 30;
    19, 10];

% Group Matrix
G = {'First Row';
    'Second Row';
    'Third Row'};

% Function
class = knnclassify(A, B, G);

% Display result 
disp('Result: ');
disp(class);



1 comment: