Left and right sides have different number of elements error (Matrices, data handling).

2 次查看(过去 30 天)
function z_hat = knnclassify_bme(y,X,T,k)
m = size(T,1);
n = size(X,1);
z_hat = zeros(m,1);
distance = zeros(n,1);
for itor = 1:m
for jtor = 1:n
distance(jtor) = pdist([X(jtor,:)';T(itor,:)']);
end
[val, ind] = sort(distance);
l = y(ind(1:k),:)';
z_hat(itor) = mode(l);
end
return
%%%%This is what the code should do
%
% Name: knnclassify_bme
%
% Inputs:
% y - A n-by-1 vector of class labels, corresponding to data points in X
% X - A n-by-p data matrix
% T - A m-by-p matrix of reference points, without/needing class labels
% k - A scalar (1-by-1) value indicating the number of nearest neighbors
% to be considered.
% Outputs:
% z_hat - A m-by-1 vector of estimated class labels for data points in T
%
% Created by: Adam C. Lammert (2020)
% Author: ??? (you)
%
% Description: Determine estimated class labels for a matrix of
% reference points T, given data points X and labels y
%%I'm not sure where my code is going wrong, but I keep getting error on the pdist line whenever I'm trying to call the function.

回答(1 个)

Sudhakar Shinde
Sudhakar Shinde 2020-9-28
编辑:Sudhakar Shinde 2020-9-28
You could try to store result in cell { } :
for jtor = 1:n
distance{jtor} = pdist([X(jtor,:)';T(itor,:)']);
end
or you could try for:
for jtor = 1:n
distance{end+1} = pdist([X(jtor,:)';T(itor,:)']);
end
you need to initilize distance={}; before starting loop.

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by