# Finding maximum and minimum in Array

Hi.

I need to find minimum and maximum in a large dataset. It is a .csv file with 14 vectors(14 analog sensors) containing around 4 minuts of logging at 1 Hz.

I have found the average lib, but i doesn't seem to work well for the example on the webpage. http://playground.arduino.cc/main/average

I know it is possible to find the minimum and maximum manual with comparing old and new values. But i find i very time consuming and inefficient for such large datasets.

So could any maybe tell me why the average lib don't work?

Or maybe has a lib like the average?

I have been spending most of the afternoon with figuring out what to do. I seems all a bit overwhelming :(

best regards bastian

Sorry, but brute force is sometimes the only way to go. Even if there was a 'find max and min command' it would still have to look at every element.

What is the origin of the .csv file ? Is the data logged by the Arduino ? If so there is a simple answer.

As usual we have to say: post your code in code tags please.

BastianGaardahl: I need to find minimum and maximum in a large dataset. It is a .csv file with 14 vectors(14 analog sensors) containing around 4 minuts of logging at 1 Hz.

So that's 240 seconds * 14 values = 3360 integer values and you want to find max/min from that?

So if logging happens at a slow data rate of only 1 Hz, why don't you remember min/max values during logging, then after finished write another file MINMAX.TXT to the SD card containing min/max values?

BastianGaardahl: But i find i very time consuming and inefficient for such large datasets.

How many KB may that be, 3360 integer values? 33 kilo bytes or less? If you chose a quick and clever file access and processing logic, it will probably take less than two seconds to find max and min values. Is that really "time consuming"?

Yes the data is logged by the arduino, and Then saved on a SD card. Is Am Award that the calculations are not time consuming, but i Can assure you that 14 different new/old values in my code makes it all very messy. I all ready had to take 2 x megas to put i all together. So yes i Am maybe lazy, but i was worth the efford if it was possible.

I Can assure you that 14 different new/old values in my code makes it all very messy.

Sounds like use of one or two arrays would tidy things up a bit.

Yes for each value i have to declare valueold and valuenew. I Am about to reach 60 global variables

As suggested, a couple of arrays would seem to be the answer.

Do you want to find the minimum and maximum of all the data? or the min and max for every channel?

With all of the memory of 2 MEGAs available to you, you could store 8000 global variables. 60 is not even trying.

Inventing names for 8000 variables is difficult. This is why arrays were invented.

I would like to find min and max for every channel :)

If you just look for the max and minimum for every channel, the code could look like this:

``````for(int i = 0; i < 14; i++)
{
if(newData[i] > maximum[i])
{
maximum[i] = newData[i];
}

if(newData[i] < minimum[i])
{
minimum[i] = newData[i];
}
}
``````

This way, you could look for the maximum and minimum during the acquisition of the data.
It would only require 3 integer arays of length 14, so 3144 = 168 Byte.
That shouldn’t be a problem on a Mega…

BastianGaardahl: I would like to find min and max for every channel :)

So that's an array of 14 max and 14 min values = 28 values = 56 Bytes

Don't you have enough RAM left so that you cannot keep the min/max values while logging, then after closing the log file write the min/max values to a file named MINMAX.TXT?

Or don't you have two seconds to read the file and find out the min/max values later?

What is the problem?

Thank you for the code example. I will try going with that. Yes i have the time to wait 2 seconds. But what i didn't had the time for was to use a whole day figuring out how to implement in in may code. But that problem is solved now thanks to a lot of useful comments and code examples. So thank you very much to you guys.