nargin in fuzzy knn
1 view (last 30 days)
Show older comments
Hello, anyone can help me please. I used fuzzy knn function in matlab, I still wondering why this function use "if nargin<7 then fuzzy=true" because if I delete this code and also the code below this program still running and there's no error. Thankyou
function [hasilDataBaru,jarakKeMasing2Hasil] = FuzzyKNN(contohData, hasilContohData, dataBaru, k, fuzzy)
if nargin<7
fuzzy = true;
end
jumlahContohData = size(contohData,1);
jumlahDataBaru = size(dataBaru,1);
m = 2;
maksHasilContohData = max(hasilContohData);
temp = zeros(length(hasilContohData),maksHasilContohData);
for i=1:jumlahContohData
temp(i,:) = [zeros(1, hasilContohData(i)-1) 1 zeros(1,maksHasilContohData - hasilContohData(i))];
end
hasilContohData = temp;
hasilDataBaru = zeros(jumlahDataBaru, length(k));
jarakKeMasing2Hasil = zeros(jumlahDataBaru, maksHasilContohData, length(k));
for i=1:jumlahDataBaru
jarak = (repmat(dataBaru(i,:), jumlahContohData,1) - contohData).^2;
jarak = sum(jarak,2)';
[~, idxTerurut] = sort(jarak);
idxContohDataTerpilih = idxTerurut(1:k);
bobot = ones(1,length(idxContohDataTerpilih));
if fuzzy,
bobot = jarak(idxContohDataTerpilih).^(-1/(m-1));
if max(isinf(bobot))
warning(['Terdapat bobot dengan nilai tak terhingga: ' num2str(i) '. Sehingga nilai bobot dikembalikan menjadi 1.']);
bobot(isinf(bobot)) = 1;
end
end
bobotPerMasing2Hasil = bobot * hasilContohData(idxContohDataTerpilih,:)/(sum(bobot)) ;
jarakKeMasing2Hasil(i,:) = bobotPerMasing2Hasil;
[~, idxMaks] = max(bobotPerMasing2Hasil');
hasilDataBaru(i) = idxMaks;
end
0 Comments
Answers (0)
See Also
Categories
Find more on Fuzzy Logic Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!