Finding minimum and maximum in Java 2D array
Ok, I've kinda fixed your code. Actually your mistake was that you have not been traversing all the cells of your multidimensional array.
So, I've added additional loop into getMinValue/getMinValue methods and fixed array elements addressing.
import java.util.*;
class MinMax {
public static void main(String[] args) {
int[][] data = {
{3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8}
};
System.out.println(getMaxValue(data));
System.out.println(getMinValue(data));
}
public static int getMaxValue(int[][] numbers) {
int maxValue = numbers[0][0];
for (int j = 0; j < numbers.length; j++) {
for (int i = 0; i < numbers[j].length; i++) {
if (numbers[j][i] > maxValue) {
maxValue = numbers[j][i];
}
}
}
return maxValue;
}
public static int getMinValue(int[][] numbers) {
int minValue = numbers[0][0];
for (int j = 0; j < numbers.length; j++) {
for (int i = 0; i < numbers[j].length; i++) {
if (numbers[j][i] < minValue ) {
minValue = numbers[j][i];
}
}
}
return minValue ;
}
}
Find min/max in a two dimensional array
Here's one way to get the min and max values:
$min = min(array_column($array, 'Price'));
$max = max(array_column($array, 'Price'));
To return the nested array for the min and max:
$prices = array_column($array, 'Price');
$min_array = $array[array_search(min($prices), $prices)];
$max_array = $array[array_search(max($prices), $prices)];
You could do each in one line since that looked like what you were trying to do:
$min_array = $array[array_search(min($prices = array_column($array, 'Price')), $prices)];
$max_array = $array[array_search(max($prices = array_column($array, 'Price')), $prices)];
PHP >= 5.5.0 needed for array_column()
or use the PHP Implementation of array_column().
Using array_map()
to get just the min and max:
$min = min(array_map(function($a) { return $a['Price']; }, $array));
$max = max(array_map(function($a) { return $a['Price']; }, $array));
There's probably a good array_filter()
or array_reduce()
as well.
Find nested min/max array in a two dimensional array
The output you wanted is strange, but this does the trick.
const arr = [[1622306648284, 1.4025036293793085],[1622309604992, 1.384071162873584],[1622312530257, 1.3503030062861177],[1622315654724, 1.3625441847416848],[1622318703104, 1.3645747739529213],[1622321575558, 1.3611235799170127],[1622324539379, 1.3750838657128996],[1622327549644, 1.378768535066251],[1622330652746, 1.3916061750979443],[1622333538315, 1.4076792700030256],[1622336466156, 1.3674852893896725]];
const max = arr.reduce((a, b) => a[1] >= b[1] ? a : b);
const min = arr.reduce((a, b) => a[1] <= b[1] ? a : b);
console.log(`Min: ${min} Max: ${max}`);
Finding the Max value in a two dimensional Array
Max of max numbers (map(max, numbers)
yields 1, 2, 2, 3, 4):
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]
>>> max(map(max, numbers)) # max of those max-numbers
4
Obtaining the min and max of a two-dimensional array using LINQ
Since Array implements IEnumerable
you can just do this:
var arr = new int[2, 2] {{1,2}, {3, 4}};
int max = arr.Cast<int>().Max(); //or Min
Find minimum and maximum value from two dimensional array
void fillUpArray(int newArray[5][5])
{
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
int randomNumber = rand() % 100 + 1;
printf("Random number[%d][%d]: %d\n", i, j, randomNumber);
newArray[i][j] = randomNumber;
}
}
}
void printMinimumMaximum(int myArray[5][5])
{
int minimum = myArray[0][0];
int maximum = myArray[0][0];
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
if (myArray[i][j] < minimum)
{
minimum = myArray[i][j];
}
if (myArray[i][j] > maximum)
{
maximum = myArray[i][j];
}
}
}
printf("Minimum: %d\n", minimum);
printf("Maximum: %d\n", maximum);
}
int main()
{
int minMax[5][5];
fillUpArray(minMax);
printMinimumMaximum(minMax);
return 0;
}
Minimum value on a 2d array python
However it only evaluates along a single axis and returns the index of the minimum value along a single row/column whereas I wish to evaluate the whole array and return the lowest value not the indices.
numpy.argmin
does not by default evaluate along a single axis, the default is to evaluate along the flattened matrix and it returns the linear index in the flattened array; from the numpy
docs that you linked:
By default, the index is into the flattened array, otherwise along the specified axis.
Either way, use numpy.amin
or numpy.min
to return the minimum value, or equivalently for an array arrname
use arrname.min()
. As you mentioned, numpy.argmin
returns the index of the minimum value (of course, you can then use this index to return the minimum value by indexing your array with it). You could also flatten into a single dimension array with arrname.flatten()
and pass that into the built-in min
function.
The four following methods produce what you want.
import numpy as np
values = np.array([
[8,2,3,4,5,6],
[3,6,6,7,2,6],
[3,8,5,1,2,9],
[6,4,2,7,8,3]])
values.min() # = 1
np.min(values) # = 1
np.amin(values) # = 1
min(values.flatten()) # = 1
Getting the min and max value in JavaScript, but from a 2D array
You can map the array to the second values of the elements:
var arr = [[[1, 112.0],[2,5.12],[3,113.1],[4,33.6],[5,85.9],[6,219.9]]];
var values = arr[0].map(function(elt) { return elt[1]; });
var max = Math.max.apply(null, values);
var min = Math.min.apply(null, values);
min max values 2D array javascript
How about flattening the array, then using Math.max.apply
and Math.min.apply
:
var arr = [[1,2,3],[4,5,6],[7,8,9]].reduce(function (p, c) {
return p.concat(c);
});
var max = Math.max.apply(null, arr); // 9
var min = Math.min.apply(null, arr); // 1
Related Topics
How to Upload Image PHP and Insert Path in MySQL
Why Can't PHP on Windows See Extension PHP_Intl.Dll Even Though It Exists
How to Add a Condition Inside a PHP Array
How to Get a List of Databases
PHP Mail() Works from Command Line But Not Apache
How to Verify the Requesting Server in PHP
In PHP, How to Generate a Big Pseudo-Random Number
How to Get the Last Dir from a Path in a String
Find All Hrefs in Page and Replace with Link Maintaining Previous Link - PHP
How to Convert an Integer to an Array in PHP
How to Integrate PHPmailer with Codeigniter 3
How to Disable PHP Magic Quotes at Runtime
Tinymce & Fancybox - Editor Won't Work on Second View