Error while calculating Cosine distance

조회 수: 2 (최근 30일)
Balaji M. Sontakke
Balaji M. Sontakke 2018년 12월 25일
편집: madhan ravi 2018년 12월 25일
Following is the error while calculating Cosine distance....
Subscripted assignment dimension mismatch.
Error in CosineDistance (line 28)
Distance(i,j) = - (ClientMean(i,:)'*EvalSet(j,:))/(norm_x*norm_y);
function Distance = CosineDistance(ClientMean, EvalSet)
% Calculate Cosine Distance
%
% Inputs:
% ClientSet ---- c* dim matrix
% EvalSet ---- n*dim matrix
% Outputs:
% Distance ---- c*n matrix
if ~exist('ClientMean','var')
error('Input arguments error.');
end
if ~exist('EvalSet','var')
error('Input arguments error.');
end
[c, d] = size(ClientMean);
[n, dim] = size(EvalSet);
if (d ~= dim)
error('Dimensionality disagreement.');
end
for i = 1 : c
for j = 1 : n
norm_x = norm(ClientMean);
norm_y = norm(EvalSet);
Distance(i,j) = - (ClientMean(i,:)'*EvalSet(j,:))/(norm_x*norm_y);
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 12월 25일
Are you sure about that code? It would make more sense if it was
ClientMean(i,:)*EvalSet(j,:)'
as that would be 1 x d * d x 1, giving 1 x 1.

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

채택된 답변

madhan ravi
madhan ravi 2018년 12월 25일
편집: madhan ravi 2018년 12월 25일
To view answer:
ClientMean=....your matrix;
EvalSet=....your matrix;
Distance = CosineDistance(ClientMean, EvalSet) % function call
celldisp(Distance) % after the calling of the function
Just change yours to below:
function Distance = CosineDistance(ClientMean, EvalSet) % function definition
% Calculate Cosine Distance
%
% Inputs:
% ClientSet ---- c* dim matrix
% EvalSet ---- n*dim matrix
% Outputs:
% Distance ---- c*n matrix
if ~exist('ClientMean','var')
error('Input arguments error.');
end
if ~exist('EvalSet','var')
error('Input arguments error.');
end
[c, d] = size(ClientMean);
[n, dim] = size(EvalSet);
if (d ~= dim)
error('Dimensionality disagreement.');
end
Distance=cell(c,n); % pre-allocate
ctr=1;
for i = 1 : c
for j = 1 : n
norm_x = norm(ClientMean);
norm_y = norm(EvalSet);
Distance{ctr} = - (ClientMean(i,:)'*EvalSet(j,:))/(norm_x*norm_y);
ctr=ctr+1;
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by