It looks like you're new here. If you want to get involved, click one of these buttons!

- 141.7K All Categories
- 104.8K Programming Languages
- 6.4K Assembler Developer
- 1.9K Basic
- 39.9K C and C++
- 4.3K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.6K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 520 Python
- 37 Ruby
- 4.3K VB.NET
- 1.6K VBA
- 20.8K Visual Basic
- 2.6K Game programming
- 312 Console programming
- 89 DirectX Game dev
- 1 Minecraft
- 110 Newbie Game Programmers
- 2 Oculus Rift
- 8.9K Applications
- 1.8K Computer Graphics
- 732 Computer Hardware
- 3.4K Database & SQL
- 524 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 257 XML Development
- 3.3K Classifieds
- 198 Co-operative Projects
- 187 For sale
- 189 FreeLance Software City
- 1.9K Jobs Available
- 600 Jobs Wanted
- 201 Wanted
- 2.9K Microsoft .NET
- 1.7K ASP.NET
- 1.1K .NET General
- 3.3K Miscellaneous
- 5 Join the Team
- 0 User Profiles
- 353 Comments on this site
- 62 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 612 Off topic board
- 177 Mobile & Wireless
- 51 Android
- 124 Palm Pilot
- 335 Multimedia
- 151 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 21 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 367 MS-DOS
- 0 Shell scripting
- 320 Windows CE & Pocket PC
- 4.1K Windows programming
- 903 Software Development
- 408 Algorithms
- 68 Object Orientation
- 89 Project Management
- 90 Quality & Testing
- 247 Security
- 7.6K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 2 Bootstrap Themes
- 55 CGI Development
- 19 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 35 JQuery
- 286 WEB Servers
- 152 WEB-Services / SOAP

If you can please help me to get this to work that would be great. thanks a lot.

[code]

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

clcq = 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

x = line(q(length(q)) + 1 : length(line));

strrep(x,'Iris-setosa','0')

strrep(x,'Iris-versicolor','1')

strrep(x,'Iris-virginica','2')

% now handle the last element

d(length(d) + 1) = str2num(x);

% put vector into matrix

D(i, = d;

% increment matrix size

i = i + 1;

end

fclose(fid);

[/code]

Terms of use / Privacy statement / Publisher: Lars Hagelin

Programmers Heaven articles / Programmers Heaven files / Programmers Heaven uploaded content / Programmers Heaven C Sharp ebook / Operated by CommunityHeaven LLC

© 1997-2015 Programmersheaven.com - All rights reserved.

## Comments

217Anyway I think that the error refers to the line

d(1) = str2num(line(1 : q(1) - 1));

because you are trying to assign a vector to a "scalar" element of d.

Morover, you can't use the word "line" as a variable because it is also a Matlab function and it could give some problems.

8The error I am getting is...

"""

??? In an assignment A(I) = B, the number of elements in B and

I must be the same.

Error in ==> get_iris2 at 31

d(length(d) + 1) = str2num(x);

"""

[code]

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')

d(length(d) + 1) = str2num(x);

% put vector into matrix

D(i, = d;

% increment matrix size

i = i + 1;

end

fclose(fid);

[/code]

217I can't understand why you try to convert this string to a number.

The function str2num convert a number in a string to a number:

str2num('5') returns 5. For a string whitout numbers it returns an empty cell []. For this reason it can't assign the empty cell to a vector element.

Maybe you use in a wrong way the lines:

strrep(x,'Iris-setosa','0')

strrep(x,'Iris-versicolor','1')

strrep(x,'Iris-virginica','2')

what is the sense of this lines?

Do you want to associate to x a different string number, '0' if x='Iris-setosa', 1 if x='Iris-versicolor, etc...?

8strrep(x,'Iris-setosa','0')

strrep(x,'Iris-versicolor','1')

strrep(x,'Iris-virginica','2')

So if you open up the data set as text, All the iris-setosa, iris-versicolor, and iris-virginica should be changed to the respected string of a number that is listed above.

That is why I use the str2num function because I want to change the "0", "1", and "2" strings to the actual number to put in the matrix.

Yes, x should change from 'iris-setosa' to '0' and so on and so forth for the others.

217These lines don't change the value of x.

strrep(x,'Iris-setosa','0')

strrep(x,'Iris-versicolor','1')

strrep(x,'Iris-virginica','2')

Because x can assume only these values, you can do in this way:

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

If I understand, in this way you solve your problem.

8I appreciate it very much!

217I'm happy for have been helpful.

8Any suggestions will help. Thanks a lot!

There is an example of the euclidean distance equation in the code.

I don't think it works correctly though. Also, what would the vectors be? 1 to 150?

[code]

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);

%%

% calculate Euclidean distance between vectors 1 and 10

% this was an example of an equation I found...

d = 0;

for i = 1 : size(D, 2) - 1

d = d + (D(1, i) - D(10, i)) ^ 2;

end

d = sqrt(d)

[/code]

217What distance do you want? In your code you made only the distance between the values of the first and the 10th rows.

Is this what you really want?

Make a numerical example on your matrix so that I can understand what you mean.

5.1 3.5 1.4 0.2 0

4.9 3 1.4 0.2 0

4.7 3.2 1.3 0.2 0

4.6 3.1 1.5 0.2 0

However your code calculate only the last distance.

Remember, when you use a for cicle ad you are interested on all the values, to do in this way:

[code]

for i = 1 : size(D, 2) - 1

d[b][color=Red](i)[/color][/b] = d[b][color=Red](i)[/color][/b] + (D(1, i) - D(10, i)) ^ 2;

end

[/code]

8So 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;

[code]

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

[/code]

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

[code]

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

[/code]

Here is the working code for "get_iris"

The iris.data file should be in the attachment for the original post

[code]

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

[/code]