Implement all nearest neighbor

조회 수: 6 (최근 30일)
ML
ML 2022년 5월 20일
편집: ML 2022년 5월 22일
Lets say I want to find all neighbors. A function that will take in an excel file with data of different cars (caryear carmileage carhorsepower carlife) amd I want to be able to input data of a car I want and if it already exist according to my input then return that but if it does not return all the colosest/similar version of that car according to some all nearest neighbor implemantation. Not sure if that makes sense but I would appreciate some help :)
  댓글 수: 4
Image Analyst
Image Analyst 2022년 5월 22일
OK, but did @bransa's Answer below work?
ML
ML 2022년 5월 22일
편집: ML 2022년 5월 22일
No I dont think I explained it well. Basically given a list of variable names find nearest neighbor.... consider min/max ranges. How can I do all this using knnsearch?

댓글을 달려면 로그인하십시오.

채택된 답변

bransa
bransa 2022년 5월 20일
편집: bransa 2022년 5월 20일
I would use logical indexing to query your excel data. When your return is empty, then prioritize the dimensions you want to search by and either use a combination of min-abs(difference) (one item searched) or knnsearch for a list of queries. Here is something to show what I am getting at. Not sure it runs bc I don't have sample data
found_index = find(carYear == inputYear & ...
carMileage == inputMileage & ...
carHorsepower == inputHorsepower & ...
carLife == inputLife);
if ~isempty(found_index)
fprintf('%s meets your input specifications.\n',carName)
else
% first try to find the year & horsepower ignoring the mileage and life
% just because this makes sense to me
found_index = find(carYear == inputYear & carHorsepower == inputHorsepower);
if ~isempty(found_index)
if length(found_index) == 1
fprintf('%s is the same year & horsepower.\n',carName)
else
fprintf('the following cars are the same year & horsepower:\n')
% now you have more than one car that meets 2 conditions.
% find the min difference for mileage and life
[~,minLifeIndex] = min(abs(carLife(found_index)-inputLife(found_index)));
[~,minMileageIndex] = min(abs(carMileage(found_index)-inputMileage(found_index)));
% note: your min~Index refers to the index of the
% array found_index. The index that meets the criteria in
% carMileage is carMileage(found_index(min~Index))
fprintf('\tclosest in life: %s\n',carName(found_index(minLifeIndex)))
fprintf('\tclosest in mileage: %s\n',carName(found_index(minMileageIndex)))
end
else
% some other less rigorous set of conditions
end
end
  댓글 수: 1
ML
ML 2022년 5월 22일
How could I implement knnsearch here?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by