How can I get my function that calculates distance between data and user input with sort data and closest data set from file

조회 수: 5 (최근 30일)
This simple function is able to get data from 'somedata.xlsx' file and takes input from user and computes the distance between them. I am wondering how I can get this function to sort the the computed distance in increasing order, and grab the first 2.
Here is the file data and my input was [2, 5, 7] and the answer is
2.4495
5.3852
4.5826
so aside from computinf I want it to return:
return increasing order
2.4495
4.5826
returned data sets
3 6 9
6 4 5
function x = computedDist(filename, input)
filedata = readmatrix (filename)
x = vecnorm(filedata - input,2,2);
% get computations increasing order
%grab and retuen first in increasing order data sets
end

채택된 답변

Stephen23
Stephen23 2022년 5월 25일
V = [2,5,7];
M = readmatrix('somedata.xlsx')
M = 3×3
3 6 9 5 7 3 6 4 5
D = vecnorm(M-V,2,2)
D = 3×1
2.4495 5.3852 4.5826
[X,Y] = mink(D,2)
X = 2×1
2.4495 4.5826
Y = 2×1
1 3
Z = M(Y,:)
Z = 2×3
3 6 9 6 4 5

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by