We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › min() returns value not in array
Page Index Toggle Pages: 1
min() returns value not in array (Read 1865 times)
min() returns value not in array
May 28th, 2010, 3:10am
 
Hi all,

I've got an array with 828 items (floats). I want to search for the max and min value. max(array_name) returns 65.8 which is correct. But min(array_name) returns 0.0, a value that isn't in the array. I don't have any empty items either in the array.

Does anybody know what's hapening here and how this can be helped?

Thanks very much in advance, best, danielle
Re: min() returns value not in array
Reply #1 - May 28th, 2010, 3:38am
 
hmm as it is working in this example :

float[] numbers = { 9.10, 1.50,23.0, 15.3323,0.355, 3.36, 3.03 };
println(max(numbers));
println(min(numbers));

it should in yours if there is nothing wrong with your array.
Do you have a way to check your array. Where do you get the data from? how do you load it ?
Re: min() returns value not in array
Reply #2 - May 28th, 2010, 5:03am
 
Along with Cedric's questions: is 0.0 lower than all the values in your array?  i.e., if 0.0 was in the array, would it be the correct min?
Re: min() returns value not in array
Reply #3 - May 28th, 2010, 5:26am
 
@Cedric, this works fine, I've also done a test with a simple array.
@Smitty, yes 0.0 would be the lowest value had it been in the array. I test the array by printing all the values with a string in front of it ("val: ") so I can see that all the items are filled. I don't get an empty val: Just val: 51.3 etc.
I load the values from a text file which starts like this:
Tijd|Decibel|Stress
10:25:12|51.6|85
10:25:02|44.1|85
10:24:52|59.7|84
10:24:42|52.6|85
10:24:32|49.8|83
10:24:22|51.2|82
10:24:12|44.8|81
10:24:02|56.1|79
10:23:52|52.1|80
10:22:32|53.5|53
10:23:42|41.8|82
10:23:32|49|84
10:23:22|46|77
10:23:02|52.1|71
10:23:12|50.9|69
10:22:52|49|72
10:22:42|54.9|62
10:25:22|51.1|78
10:25:32|54.7|80
... etc
The summarized code:
Code:

// load data from file
String [] rows = loadStrings(filename);
float [] db_val = new float[rows.length];
// I start the for loop at 1 to skip the text line
 for(int i=1; i<rows.length; i++)
 {
   String tmp_val[] = split(rows[i], "|");
   // get second column
   float d = float(tmp_val[1]);
   // store db values in array
   db_val[i] = d;
   println ("val: "+d);
   float db_max = max(db_val);
   float db_min = min(db_val);
   print("max: ");
   println(db_max); // 65.8
   print("min: ");
   print(db_min); // 0.0
 }

Oh, I just printed db_val[0] and it returns 0.0 ....
Now I try: db_val[i-1] = d; // db_val[0] = 51.6 which is correct but the min() still returns 0.0

Hope you can help, tia.
Re: min() returns value not in array
Reply #4 - May 28th, 2010, 5:48am
 
danielle wrote on May 28th, 2010, 5:26am:
Oh, I just printed db_val[0] and it returns 0.0 ....
Now I try: db_val[i-1] = d; // db_val[0] = 51.6 which is correct but the min() still returns 0.0

You've got the right idea.  The code you quoted never sets db_val[0], so it remains 0.0.  But when you change to db_val[i-1] = d; you leave the last element of db_val untouched, so it remains 0.0, which is then the minimum.

The solution is to keep the second version and use:

Code:
float [] db_val = new float[rows.length-1]; 



which gets you an array of exactly the right length.
Re: min() returns value not in array
Reply #5 - May 28th, 2010, 5:56am
 
Great, I've got it now, thanks.
I was searching just now for an easy way to print the array content. Like you do in PHP with var_dump or just a trace statement in ActionScript. Is there a method or function in Processing to do this?

Thanks again, d
Re: min() returns value not in array
Reply #6 - May 28th, 2010, 6:08am
 
Code:
float[] numbers = { 9.10, 1.50,23.0, 15.3323,0.355, 3.36, 3.03 };
println(numbers);

One of the many convenient shortcuts provided by Processing... Smiley
Re: min() returns value not in array
Reply #7 - May 28th, 2010, 6:30am
 
Smiley
OK, pfff, I did print(array_name) and got gibberish but println gives a nice overview. Thanks.
Page Index Toggle Pages: 1