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]

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

- 140.8K All Categories
- 103.6K Programming Languages
- 6.4K Assembler Developer
- 401 Assembly Code Share
- 239 Getting started in assembly
- 4.6K x86 Assembly
- 1.9K Basic
- 97 Qbasic
- 39.9K C and C++
- 5.6K Beginner C/C++
- 330 C/C++ on Linux/Unix
- 450 C/C++ Windows API
- 522 C++ Builder
- 253 C++ Game Development
- 3.3K C++ MFC
- 103 C++.NET
- 404 Visual C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 334 Advanced Delphi
- 360 Delphi beginners
- 4 Haskell
- 9.7K Java
- 56 Enterprise JavaBeans
- 1.3K Java Beginners
- 304 Java Server Pages
- 4.1K Pascal
- 1.3K Perl
- 11 Perl 6
- 2K PHP
- 546 Python
- 37 Ruby
- 4.4K VB.NET
- 258 Advanced VB.Net
- 1.6K VBA
- 20.8K Visual Basic
- 767 Access databases and VB
- 831 Advance Visual Basic
- 1.2K Beginner VB
- 2.6K Game programming
- 315 Console programming
- 90 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 279 3D Graphics
- 129 DirectX
- 125 OpenGL
- 740 Computer Hardware
- 9 Cooling & Overclocking
- 3.4K Database & SQL
- 1.1K Access
- 91 ADO Programming
- 288 MySQL
- 358 Oracle
- 440 SQL-Server
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 25 DirectSound
- 257 XML Development
- 3.3K Classifieds
- 199 Co-operative Projects
- 198 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 603 Jobs Wanted
- 209 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 22 .NET WEB-Services
- 129 .NET WinForms
- 14 .NET XML
- 50 ADO.NET
- 142 C# & VB.NET School Support
- 3.4K Miscellaneous
- 4 Join the Team
- 354 Comments on this site
- 69 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 621 Off topic board
- 200 Mobile & Wireless
- 72 Android
- 126 Palm Pilot
- 338 Multimedia
- 154 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 27 Cloud Computing
- 1 Witsbits Go Cloud
- 53 FreeBSD
- 1.7K LINUX programming
- 1 Awk scripting
- 332 Linux Support
- 0 Sed scripting
- 370 MS-DOS
- 0 Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 177 COM/DCOM
- 61 Networking And Security
- 17 Windows 2003 Server
- 6 Windows Vista
- 176 Windows XP
- 939 Software Development
- 416 Algorithms
- 68 Object Orientation
- 24 RUP & UML
- 91 Project Management
- 95 Quality & Testing
- 268 Security
- 63 Evil Scripting
- 81 Hacking
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 4 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 131 Mobile Internet & Messaging
- 211 Wireless development
- 2.2K JavaScript
- 37 JQuery
- 304 WEB Servers
- 153 Apache
- 79 IIS
- 150 WEB-Services / SOAP

## Comments

Anyway 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.

The 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]

I 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...?

strrep(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.

These 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.

I appreciate it very much!

I'm happy for have been helpful.

Any 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]

What 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]

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;

[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]

I can try with an example.

x=[1 1]

y=[1 2]

the euclidean distance is d=sqrt((x(1,1)-y(1,1))^2+(x(1,2)-y(1,2))^2);

in 3 dimension:

x1=[1 1 1]

y1=[1 1 2]

the euclidean distance is d=sqrt((x1(1,1)-y1(1,1))^2+(x1(1,2)-y1(1,2))^2+(x1(1,3)-y1(1,3))^2);

so, if x2 and y2 contain more than a vector:

x2 = [1 2 3; 4 5 6; 7 8 9]

y2 = [ 8 3 4; 5 4 6; 5 6 7]

What I understand is that you want calculate the distance between

[1 2 3] and [8 3 4]

[4 5 6] and [5 4 6]

[7 8 9] and [5 6 7]

so

[code]

for i=1:size(x1,1)

d(i)=sqrt((x1(i,1)-y1(i,1))^2+(x1(i,2)-y1(i,2))^2+(x1(i,3)-y1(i,3))^2);

end

[/code]

Now d contains the 3 distances. (is what you wanted?)

the minimum distance is simply the minimum value of d, so

[code]

min_dist=min(d);

[/code]

THE CODE U GIVEN IS STILL HAVING A PROBLEM. IT WONT RUN PROPERLY. CAN U SUGGEST ME CODE FOR THIS