views:

171

answers:

1

When doing:

load training.mat
training = G

load testing.mat
test = G

and then:

>> knnclassify(test.Inp, training.Inp, training.Ltr)

??? Error using ==> knnclassify at 91
The length of GROUP must equal the number of rows in TRAINING.

Since:

>> size(training.Inp)
ans =
          40          40        2016

And:

>> length(training.Ltr)
ans =
        2016

How can I give the second parameter of knnclassify (TRAINING) the training.inp 3-D matrix so that the number of rows will be 2016 (the third dimension)?

+2  A: 

Assuming that your 3D data is interpreted as 40-by-40 matrix of features for each of the 2016 instances (third dimension), we will have to re-arrange it as a matrix of size 2016-by-1600 (rows are samples, columns are dimensions):

%# random data instead of the `load data.mat`
testing = rand(40,40,200);
training = rand(40,40,2016);
labels = randi(3, [2016 1]);     %# a class label for each training instance
                                 %# (out of 3 possible classes)

%# arrange data as a matrix whose rows are the instances,
%# and columns are the features
training = reshape(training, [40*40 2016])';
testing = reshape(testing, [40*40 200])';

%# k-nearest neighbor classification
prediction = knnclassify(testing, training, labels);
Amro

related questions