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

2 ビュー (過去 30 日間)
Frisda Sianipar
Frisda Sianipar 2021 年 4 月 28 日
コメント済み: Frisda Sianipar 2021 年 5 月 4 日
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 件のコメント
David Fletcher
David Fletcher 2021 年 4 月 28 日
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?
Frisda Sianipar
Frisda Sianipar 2021 年 4 月 29 日
there are three column

サインインしてコメントする。

採用された回答

David Fletcher
David Fletcher 2021 年 4 月 29 日
編集済み: David Fletcher 2021 年 4 月 29 日
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 件のコメント
Frisda Sianipar
Frisda Sianipar 2021 年 5 月 4 日
i will try sir
Frisda Sianipar
Frisda Sianipar 2021 年 5 月 4 日
it still have error sir

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by