Matrix dimensions for Euclidean Distance

I am working on project "Speaker Recognition using PNCC and MFCC". For recognition I have to do matching step . For which I have to compare the feature matrix of test voice sample with the feature matrix of the rest of the voice samples. Matching is done using euclidean distance between the test sample and the samples in database. For MFCC my code works correctly. However, for PNCC the code might seem to change the dimensions and an error: "Matrix dimensions must agree" is generated. My question is that how to find the euclidean distance with different dimensions or how to change the dimensions such that my results are not affected.
My CODE:
function d = disteu(x, y)
% DISTEU Pairwise Euclidean distances between columns of two matrices
%
% Input:
% x, y: Two matrices whose each column is an a vector data.
%
% Output:
% d: Element d(i,j) will be the Euclidean distance between two
% column vectors X(:,i) and Y(:,j)
%
% Note:
% The Euclidean distance D between two vectors X and Y is:
% D = sum((x-y).^2).^0.5
% D = sum((x-y).^2).^0.5
[M, N] = size(x);
[M2, P] = size(y);
if (M ~= M2)
error('Matrix dimensions do not match.')
end
d = zeros(N, P);
if (N < P)
copies = zeros(1,P);
for n = 1:N
d(n,:) = sum((x(:, n+copies) - y) .^2, 1);
end
else
copies = zeros(1,N);
for p = 1:P
d(:,p) = sum((x - y(:, p+copies)) .^2, 1)';
end
end
d = d.^0.5;

4 Comments

  1. Please post a copy of the complete error message. This would reveal, which line is failing.
  2. What are the inputs x and y in your case?
  3. sqrt(x) is faster than x.^0.5.
  4. Since Matlab R2016b you can omit the "+copies".
My code is too long. the dimensions keep changing for x and y in every iteration
the error is
M =
335
N =
80
M2 =
281
P =
16
Matrix dimensions must agree.
Error in disteu (line 50)
d(:,p) = sum((x - y(:, p+copies)) .^2, 1)';
Error in test (line 21)
d = disteu(v, code{l});
And the inputs x and y come from 2 different functions used in my code.I have attached the code. You have to run main.m
Error: Matrix dimensions must agree.
Error in disteu (line 50) d(:,p) = sum((x - y(:, p+copies)) .^2, 1)';
Error in test (line 21) d = disteu(v, code{l});
Error in main (line 5) test('C:\Users\Bisma Waheed\Desktop\pncc\pncc plus mfcc\mfcc_vq\test2\',5, code);

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 25 Mar 2018
Edited: Matt J on 25 Mar 2018
Rather than debugging disteu, I invite you to use my own implementation (attached). The help doc should be pretty straightforward.

4 Comments

Is this used to find the Euclidean Distance between feature vectors? And i really thank you for the help
Matt J
Matt J on 25 Mar 2018
Edited: Matt J on 25 Mar 2018
Is this used to find the Euclidean Distance between feature vectors?
It does exactly what disteu is intended to do, with a few more options. Try it out and see the help doc.
And i really thank you for the help
You're quite welcome, but please click 'Accept' if this does what you need.
I am still getting an error quite similar to the previous one, Error using reshape Product of known dimensions, 314, not divisible into total number of elements, 5360.
Error in disteu (line 37) y=reshape(y,N,1,[]);
Error in test (line 21) d = disteu(v, code{l});
Hi, I have the same error, have you solved this problem?
Thanks.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!