nargin in fuzzy knn
조회 수: 3 (최근 30일)
이전 댓글 표시
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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Clustering에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!