Maximum value of an array
-
What is the quickest way to determine the maximum value in an array. I have a two dimensional array, let's say, SampleArray[17, 127]. For each of the 17 sub-arrays I want to find the maximum of the 127 samples. Thanks for your help.
-
What is the quickest way to determine the maximum value in an array. I have a two dimensional array, let's say, SampleArray[17, 127]. For each of the 17 sub-arrays I want to find the maximum of the 127 samples. Thanks for your help.
Either use
Array.Sort
with the 2nd dimension of each 1st dimension and grab the last element, or enumerate (or iterate) through each one, compare values. If the next value is higher than the current, store the the next value. Either way, continue. Both are O(n) operations but unless your array is already sorted, there is no faster algorithm.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Either use
Array.Sort
with the 2nd dimension of each 1st dimension and grab the last element, or enumerate (or iterate) through each one, compare values. If the next value is higher than the current, store the the next value. Either way, continue. Both are O(n) operations but unless your array is already sorted, there is no faster algorithm.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Hello again, Heath! I used the following code...is this acceptable? for (int i = 0; i < 17; i++) { for (ka = PosCtr; ka < 125; ka++) { MaxVal = 0; MaxVal = Math.Max(MaxVal, AutocorrArray[i, ka]); } PeakValueArray[i] = MaxVal; }
-
Hello again, Heath! I used the following code...is this acceptable? for (int i = 0; i < 17; i++) { for (ka = PosCtr; ka < 125; ka++) { MaxVal = 0; MaxVal = Math.Max(MaxVal, AutocorrArray[i, ka]); } PeakValueArray[i] = MaxVal; }
Shouldn't 125 be 127? I'm not sure what
PosCtr
is either, but you could probably just hardcode 0 to start at the beginning of the sub-array. You also shouldn't reset MaxVal each time. Just do something like this:for (int i=0; i < 17; i++)
{
int max = 0;
for (int j=0; j < 127; j++)
max = Math.Max(max, arr[i, j]);
peekValues[i] = max;
}If you reset
MaxVal
(ormax
in my example), then the value will always be the max because it's greater than 0. This means thatAutocorrArray[i, 126]
will be the max value whether or not it is.Microsoft MVP, Visual C# My Articles
-
Shouldn't 125 be 127? I'm not sure what
PosCtr
is either, but you could probably just hardcode 0 to start at the beginning of the sub-array. You also shouldn't reset MaxVal each time. Just do something like this:for (int i=0; i < 17; i++)
{
int max = 0;
for (int j=0; j < 127; j++)
max = Math.Max(max, arr[i, j]);
peekValues[i] = max;
}If you reset
MaxVal
(ormax
in my example), then the value will always be the max because it's greater than 0. This means thatAutocorrArray[i, 126]
will be the max value whether or not it is.Microsoft MVP, Visual C# My Articles
PosCtr is just a counter variable that starts at 31. It starts at 31 and not 0 because I'm not looking at the first eighth of an initial record of length 256. When I tried your approach I had the following problem: The max values detected are wrong, and the position is either 122, 123 or 124. When I used my code: Whenever there was a sequence of negative numbers followed by a series of positive numbers, the max value recorded is 0 :mad: What am I doing wrong?
-
PosCtr is just a counter variable that starts at 31. It starts at 31 and not 0 because I'm not looking at the first eighth of an initial record of length 256. When I tried your approach I had the following problem: The max values detected are wrong, and the position is either 122, 123 or 124. When I used my code: Whenever there was a sequence of negative numbers followed by a series of positive numbers, the max value recorded is 0 :mad: What am I doing wrong?
The whole point of using
Math.Max
is to compare the current array value with the previous max value. Whichever one is greater gets stored as the max value and compared to the next value, and so on. My code works under normal circumstances, but it might not work for the constraints you have (like your starting indexes, etc.). You must not reset the max each time, though, otherwise you're comparing the current array value with 0, so the last positive number in your array (or subset - whatever you're comparing) will be stored as the max value. You must remember it as I did in my example so you only store the max. If you want to take negative numbers into account, set the max value initially toInt32.MinValue
.Microsoft MVP, Visual C# My Articles