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

2 Ansichten (letzte 30 Tage)
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.

Antworten (1)

Sudhakar Shinde
Sudhakar Shinde am 28 Sep. 2020
Bearbeitet: Sudhakar Shinde am 28 Sep. 2020
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.

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by