How about if instead, I make a function that contains the euclidean_distance code so that it goes over all of the compenents of x and y (x and y being vectors).
So if I take your code and modify it a little, it might look like the code section below?
I'm just not sure what to do with this part.
What would I put in place of the "?" and are the x and y in this equation below in the correct places?
The answer for the output should be this if you run it with the x and y given in the code: 7.3485 2.4495 2.2361
d = 0;
for i = 1 : size(x, 2) - 1
d(i) = d(i) + (x(1, i) - y(?, i)) ^ 2;
function d = euclidean_distance (x, y)
% This wouldn't be in the actual code
% Only here for testing purposes to test the equation below
x = [1 2 3; 4 5 6; 7 8 9]
y = [ 8 3 4; 5 4 6; 5 6 7]
d = 0;
for i = 1 : size(x, 2) - 1
d(i) = d(i) + (x(1, i) - y(10, i)) ^ 2;
end
d = sqrt(d)
return
After this is executed, I will have another file that will find the two nearest points. So I believe I need two for loops so it goes over all of the data points and then compares it to all of the other data points. Also I would call the euclidean_distance function from above inside these loops somehow so that the equation is finding the nearest points of the output form the euclidean_distance function.
Is this clear... sorry if I'm not good at explaining.
This code might start of like the code below but has to call the euclidean function from above some how.
This code is of course only for finding the minimum of one vector so another for loop will have to be put in which would also call the euclidean function I believe
D = get_iris()
n = size(a); % variable "a" being a single vector
min = 0;
for i = 1 : 1 : n
if a(i) < m
m = a(i);
end
end
Here is the working code for "get_iris"
The iris.data file should be in the attachment for the original post
function D = get_iris ()
fid = fopen('iris.data', 'rt');
if (fid == -1)
error(['The file cannoot be opened!']);
end
i = 1;
while feof(fid) == 0
%grab line from file
line = fgetl(fid);
% find all commas
q = find(line == ',');
% extract numbers
d = [];
d(1) = str2num(line(1 : q(1) - 1)); % separately do first element
% now handle all middle elements
for j = 2 :length(q)
d(j) = str2num(line(q(j - 1) + 1 : q(j) - 1));
end
% now handle the last element
x = line(q(length(q)) + 1 : length(line));
%strrep(x,'Iris-setosa','0')
%strrep(x,'Iris-versicolor','1')
%strrep(x,'Iris-virginica','2')
switch x
case 'Iris-setosa'
d(length(d) + 1) = 0
case 'Iris-versicolor'
d(length(d) + 1) = 1
case 'Iris-virginica'
d(length(d) + 1) = 2
end
%d(length(d) + 1) = str2num(x);
% put vector into matrix
D(i, :) = d;
% increment matrix size
i = i + 1;
end
fclose(fid);
return