wrong size of matrix

2 views (last 30 days)
Kamil Kacer
Kamil Kacer on 23 Nov 2020
Answered: Kamil Kacer on 23 Nov 2020
HI guys when i run this function it gives me an error This is F
F
fileClassification('audio.wav', 1, 'auto5.mat')
Error using pdist2 (line 309)
The covariance matrix for the Mahalanobis metric must be a square matrix with the same number of columns
as X. And it must be symmetric and positive definite.
Error in classifyKNN_D_Multi (line 54)
d{i} = pdist2(F{i}.', testSample, 'mahalanobis'); %56
Error in fileClassification (line 43)
[P, label] = classifyKNN_D_Multi(Features, ...
>>
function [Ps, winnerClass] = classifyKNN_D_Multi(F, testSample, k, NORMALIZE, distancetype )
% function [Ps, winnerClass] = classifyKNN_D_Multi(F, testSample, k, NORMALIZE, distancetype);
%
% This function is used for classifying an unknown sample using the kNN
% algorithm, in its multi-class form.
%
% ARGUMENTS:
% - F: an CELL array that contains the feature values for each class. I.e.,
% F{1} is a matrix of size numOfDimensions x numofSamples FOR THE FIRST
% CLASS, etc.
%
% - testSample: the input sample to be classified
% - k: the kNN parameter
% - NORMALIZE: use class priors to weight results
% - useL1distance: use L1 instead of L2 distance
%
% RETURNS:
% - Ps: an array that contains the classification probabilities for each class
% - winnerClass: the label of the winner class
%%error(nargchk(4,5,nargin))
switch nargin
case 4
distancetype = 1; % euclidean distance if 4 variables included
case 5
distancetype = 3;
otherwise
disp('error')
end
numOfClasses = length(F);
if (size(testSample, 2)==1)
testSample = testSample';
end
% initilization of distance vectors:
numOfDims = zeros( 1, numOfClasses );
numOfTrainSamples = zeros( 1, numOfClasses );
d = cell(numOfClasses,1);
% d{i} is a vector, whose elements represent the distance of the testing
% sample from all the samples of i-th class
testSample(isnan(testSample)) = 0.0;
for i=1:numOfClasses
[ numOfDims(i), numOfTrainSamples(i) ] = size( F{i} );
d{i} = inf*ones(max(numOfTrainSamples), 1); % we fill it with inf values
F{i}(isnan(F{i})) = 0.0;
end
if (length(testSample)>1)
for i=1:numOfClasses % for each class:
if (numOfTrainSamples(i)>0)
if ( distancetype == 1 )
% d{i} = sum( abs(repmat(testSample, [numOfTrainSamples(i) 1]) - F{i}'),2); % L1
d{i} = pdist2(F{i}.', testSample, 'euclidean');
elseif ( distancetype == 2)
d{i} = pdist2(F{i}.', testSample, 'cityblock'); %L1
else
d{i} = pdist2(F{i}.', testSample, 'mahalanobis'); %56
end
d{i} = sort(d{i});
d{i}(end+1:max(numOfTrainSamples)) = inf;
else
d{i} = inf;
end
end
else % single dimension (NO SUM required!!!)
for i=1:numOfClasses
if (numOfTrainSamples(i)>0)
d{i} = (abs(repmat(testSample, [numOfTrainSamples(i) 1]) - F{i}')');
d{i} = sort(d{i});
d{i}(end+1:max(numOfTrainSamples)) = inf;
else
d{i} = inf;
end
end
end
kAll = zeros(numOfClasses, 1);
for j=1:k
curArray = zeros(numOfClasses, 1);
for i=1:numOfClasses
curArray(i) = d{i}(kAll(i)+1);
end
[MIN, IMIN] = min(curArray);
kAll(IMIN) = kAll(IMIN) + 1;
end
if ( NORMALIZE == 0 )
Ps = (kAll ./ k);
else
Ps = kAll ./ numOfTrainSamples';
Ps = Ps / sum(Ps);
end
[MAX, IMAX] = max(Ps);
winnerClass = IMAX;
  1 Comment
dpb
dpb on 23 Nov 2020
What is testsample?
From function comments doesn't look like it would necessarily be the matching Y to go with X which seems to be what the error is telling you.

Sign in to comment.

Answers (1)

Kamil Kacer
Kamil Kacer on 23 Nov 2020
[Features, classNames, MEAN, STD, ...
stWin, stStep] = kNN_model_load(modelFileName);
[x, fs] = audioread(wavFileName); % read wav file
% short-term feature extraction:
ShortFeatures = stFeatureExtraction(x, fs, stWin, stStep);
%mtWinRatio = mtWin / stWin; mtStepRatio = mtStep / stStep;
% mid-term feature statistic calculation:
%[mtFeatures] = mtFeatureExtraction(...
% stF, mtWinRatio, mtStepRatio, Statistics);
% long term averaging of the mid-term statistics:
ShortFeatures = mean(ShortFeatures,2);
% kNN classification
% % [P, label] = classifyKNN_D_Multi(Features, ...
% % (mtFeatures - MEAN') ./ STD', kNN, 1);
[P, label] = classifyKNN_D_Multi(Features, ...
(ShortFeatures - MEAN') ./ STD', kNN,1,1);
ShortFeatures is test sample

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!