Index in position 2 exceeds array bounds (must not exceed 1)

i get error on this code, can anybody help me please?
The error: Index in position 2 exceeds array bounds (must not exceed 1).
Error in KNN (line 3)
group=latih(:,3);
the code :
x=xlsread("datatraining.xlsx");
latih=x;
group=latih(:,3);
latih = [latih(:,1) latih(:,2)];
for i = 1 : 80
y=xlsread("datatesting.xlsx");
sampel = y;
test = [sampel(:,1) sampel(:,2)];
%sampel = [2.6136 0.1284 1.3017 -0.8089 0.0441 -0,2084];
hasil=knnclassify(test,latih,group);
end
nama = "hasil KNN.xlsx";
hasil = [sampel(:,1) sampel(:,2) sampel(:,3) hasil];
xlswrite(nama,hasil);
Thankyou in advance

2 Comments

The error would suggest that for whatever reason the data you have read consists of only a single column (hence you get the error when trying to index the third column). Look at latih in the workspace - what size does it say it is?

Sign in to comment.

 Accepted Answer

OK - if you look at the documentation of xlsread, it says the function is no longer recommended to be used. Better to use readtable instead.
x=readtable("datatraining.xlsx");
latih=x;
group=latih(:,3);
latih = [latih(:,1) latih(:,2)];
for i = 1 : 80
y=readtable("datatesting.xlsx");
sampel = y;
test = [sampel(:,1) sampel(:,2)];
%sampel = [2.6136 0.1284 1.3017 -0.8089 0.0441 -0,2084];
hasil=knnclassify(test,latih,group);
end
nama = "hasil KNN.xlsx";
hasil = [sampel(:,1) sampel(:,2) sampel(:,3) hasil];
xlswrite(nama,hasil);
As to your other code, I can't comment as I don't have the toolbox on hand with knnclassify. The use of xlswrite (at the end of your code) is also deprecated, so may possibly also produce unpredictable results (as you found with xlsread)

7 Comments

i have tried sir, but the error become different
I don't have that toolbox, but again looking at the docs, knnclassify was dropped from the Bioinformatics toolbox as of R2017. They suggest using fitcknn to create a knn model and predict to classify data using the knn model from fitcknn.
sir,i'm sorry to disturb your time. Could you help me to answer my question about "how to convert knnclassify to fitcknn" sir?
Thannkyou
Looking at the docs it would be something like this:
x=readtable("datatraining.xlsx");
latih=x;
group=latih(:,3);
latih = [latih(:,1) latih(:,2)];
for i = 1 : 80
y=readtable("datatesting.xlsx");
sampel = y;
test = [sampel(:,1) sampel(:,2)];
%sampel = [2.6136 0.1284 1.3017 -0.8089 0.0441 -0,2084];
%Create knn model based on default parameters
%**Take note: the classifier model is likely to be need optimising by
% experimenting with the options.
classifierModel=fitcknn(latih,group,'CategoricalPredictors','all');
%use the knn model to predict classes for test data
predictedClasses=predict(classifierModel,test)
end
Be aware however that there are a lot of options that are likely to affect the performance of the classifier. You use this at your own risk - these models are not my area of expertise, and I do not even have the toolbox to test to see if it works. This was written purely from looking at the docs for fitcknn and predict.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!