Euclidean distance between two structs for nearest neighbour

์กฐํšŒ ์ˆ˜: 4 (์ตœ๊ทผ 30์ผ)
Aaron Elliott
Aaron Elliott 2020๋…„ 11์›” 9์ผ
๋Œ“๊ธ€: Jason Reed 2020๋…„ 11์›” 11์ผ
Hi all,
I am trying to do nearest neighbour between a set of images and a nearest neighbour model. However in my euclidean distance function I am getting errors suh as "matrix dimensions must agree".
As you can see i've attemped different ways but what im trying to do for these two functions is:
โ€ข Calculate the Euclidean distance between the test sample and all the training samples d(๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’1,๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’2)=|๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’1โˆ’๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’2|=โˆš(๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’1(1)โˆ’๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’2(1))2+ (๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’1(2)โˆ’๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’2(2))2+โ‹ฏ+(๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’1(๐‘›)โˆ’๐‘ ๐‘Ž๐‘š๐‘๐‘™๐‘’2(๐‘›))2
โ€ข Select the closest training example
โ€ข Assign the closest training exampleโ€™s label to the test image
function dEuc = EuclideanDistance(sample1,sample2)
% dEuc = sqrt(sum((sample1 - sample2).^2));
% dEuc = norm(sample1 - sample2);
% for i = length(sample1)
% for j = length(sample2)
% num = sum((sample1(:) - sample2(:)).^2);
% end
% end
% dEuc = sqrt(num);
% sample1 = repmat(sample1,1,size(sample2,2));
% dEuc = sqrt(sum((sample1(:)-sample2(:)).^2));
V = sample1 - sample2;
dEuc = sqrt(V .* V');
end
function prediction = NNTesting(testImage,modelNN)
dataset = modelNN.neighbours;
prediction = EuclideanDistance(testImage, dataset);
end

๋‹ต๋ณ€ (1๊ฐœ)

KSSV
KSSV 2020๋…„ 11์›” 9์ผ
ํŽธ์ง‘: KSSV 2020๋…„ 11์›” 9์ผ
dEuc = sqrt(V .* V');
Replace the above with
dEuc = sqrt(sum(V.^2));
%% Demo
A = rand(100,2) ; B = rand(100,2) ;
dx = A-B ;
d = sqrt(sum(dx.^2,2)) ;
% Formula
d1 = sqrt((A(:,1)-B(:,1)).^2+(A(:,2)-B(:,2)).^2) ;
isequal(d,d1)
You have to use sum with 1 or 2 depending on your data is row major or column major.
  ๋Œ“๊ธ€ ์ˆ˜: 6
Aaron Elliott
Aaron Elliott 2020๋…„ 11์›” 11์ผ
Hahaha I did, just thought to ask on here as well just have to options
Jason Reed
Jason Reed 2020๋…„ 11์›” 11์ผ
Ah, fair! Did he respond? I'm not exactly here because I figured this out haha

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Pattern Recognition and Classification์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

Community Treasure Hunt

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

Start Hunting!

Translated by