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