ntrain=max(size(train_val)); % number of training data
rmse=0;                      % Initialize RMSE to 0
for i = 1:ntrain             % For each training point
	val = train_val(i,1);           % TV value for this point
	dist=abs(data_val(:,1)-val);    % Computes distance to all points in DATA
	[t,idx]=sort(dist);             % Sort these distances
	y=data_val(idx,2);              % Order the SALES value in DATA set accordingly
	y_predict(i) = y(1);            % This is a 1-NN: pick the first value
	rmse = rmse + (train_val(i,2)-y(1)).^2;   % Update RMSE: add square of differences
end                             % end loop
rmse = sqrt(rmse/ntrain);    % Compute RMSE
figure
plot(train_val(:,2),y_predict,'or','LineWidth',1.5);
xlabel('Real value for sales of training dataset')
ylabel('Predicted value for sales of training dataset')
title("RMSE = " + rmse);
