Questions tagged [max]

Maximum value. Largest, biggest, greatest.

max

max() is a math library function available in many programming environments which returns the entity of greatest value from two or more candidate values.

Do not use for questions about the maximal value of a type, e.g. Long.MAX_VALUE, Integer.MAX_VALUE, etc.

Examples

SQL

SQL MAX() function is one of aggregate functions. It returns the largest value of column.

SELECT MAX(column_name) FROM table_name;

Python

In Python, the native max function identifies the largest element of a set (e.g. a list).

>> max([1, 2, 3])
3

Java

In Java we can use java.lang.Math class to compute maximum value of two arguments.

int maxElement = Math.max(x, y);

To compute maximum element in collection we can use, max() method from java.util.Collections class.

int maxElement = Collections.max(list);

Ruby

In Ruby to compute maximum element of collection we simply use max function.

[4,7].max

Clojure

In Clojure we use max function in the following way.

(max 1 2 3 4)

In above code max is the function name and numbers 1..4 are arguments passed to it. We can also apply the max function to lists.

(apply max [1 2 3 4])

R

In R we have function max.

x <- c(1, 2, 3)
y <- c(4, 5)
z <- c(6, NA)
max(x)
max(y)
max(z)
max(z, na.rm = TRUE)
max(x, y, z, na.rm = TRUE)  ## as same as max(c(x, y, z), na.rm = TRUE)

Rust

In Rust we can use std::cmp::max to compute the maximum value of two arguments.

let max = std::cmp::max(x, y);

We can compute the maximum element in a collection by transforming it into an Iterator and then calling the max method on the iterator.

let max = collection.into_iter().max();
8982 questions
37
votes
2 answers

TensorFlow: Max of a tensor along an axis

My question is in two connected parts: How do I calculate the max along a certain axis of a tensor? For example, if I have x = tf.constant([[1,220,55],[4,3,-1]]) I want something like x_max = tf.max(x, axis=1) print sess.run(x_max) output:…
aphdstudent
  • 545
  • 1
  • 5
  • 13
37
votes
2 answers

C extension: ? operators

I observed that there was at some point a ? operator in GCC. How can I use these under GCC 4.5? Have they been removed, and if so, when? Offset block_count = (cpfs->geo.block_size - block_offset)
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
37
votes
9 answers

Fastest way to zero out low values in array?

So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this should be set to 0. What would be the fastest way to do this in Python? …
David
  • 383
  • 1
  • 3
  • 5
37
votes
2 answers

Image mysteriously ignoring max-width in Firefox & IE

So I'm trying to display images as big as possible with no cropping on any screen size. This means height: 100%; width: auto in landscape and width: 100%; height: auto in portrait. I'm serving up images that are big enough to fill retina iPads so…
suryanaga
  • 3,728
  • 11
  • 36
  • 47
37
votes
2 answers

How set 0 with MAX function when it is NULL?

I would like to understand how to set 0 value of the attribute when it is NULL with MAX function. For example: Name columns: number - date Values: 10 - 2012-04-04 11 - 2012-04-04 12 - 2012-04-04 13 - 2012-04-15 14 - 2012-06-21 1 -…
Donovant
  • 3,091
  • 8
  • 40
  • 68
36
votes
7 answers

Taking the record with the max date

Let's assume I extract some set of data. i.e. SELECT A, date FROM table I want just the record with the max date (for each value of A). I could write SELECT A, col_date FROM TABLENAME t_ext WHERE col_date = (SELECT MAX (col_date) …
Revious
  • 7,816
  • 31
  • 98
  • 147
36
votes
3 answers

List minimum in Python with None?

Is there any clever in-built function or something that will return 1 for the min() example below? (I bet there is a solid reason for it not to return anything, but in my particular case I need it to disregard None values really bad!) >>> max([None,…
c00kiemonster
  • 22,241
  • 34
  • 95
  • 133
36
votes
1 answer

Using min/max *within* an Integer Linear Program

I'm trying to set up a linear program in which the objective function adds extra weight to the max out of the decision variables multiplied by their respective coefficients. With this in mind, is there a way to use min or max operators within the…
solvingPuzzles
  • 8,541
  • 16
  • 69
  • 112
35
votes
3 answers

MySQL: Select top n max values?

I am really confused about the query that needing to return top N rows having biggest values on particular column. For example, if the rows N-1, N, N + 1 have same values. Must I return just top N or top N + 1 rows.
Hoa Vu
  • 2,865
  • 4
  • 25
  • 33
34
votes
4 answers

Find the maximum and minimum value of every column and then find the maximum and minimum value of every row

I've got this matrix: a <- matrix(rnorm(1000 * 18, mean = 100, sd = sqrt(10)), 1000, 18) I would like to find the maximum and minimum value of every column and the maximum and minimum value of every row.
Kostas Dimakis
  • 1,143
  • 1
  • 10
  • 20
34
votes
4 answers

Sort Pandas dataframe and print highest n values

I have a pandas data frame and I want to sort column('Bytes') in Descending order and print highest 10 values and its related "Client IP" column value. Suppose following is a part of my dataframe. I have many different methods and failed? 0 …
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121
34
votes
4 answers

What's the maximum size of a numpy array?

I'm trying to create a matrix containing 2 708 000 000 elements. When I try to create a numpy array of this size it gives me a value error. Is there any way I can increase the maximum array size? a=np.arange(2708000000) ValueError …
branwen85
  • 1,606
  • 5
  • 20
  • 25
33
votes
3 answers

What is the maximum length of an NSString object?

What is the maximum sized string that can be held in a NSString object? Does this change dynamically?
Vipin
  • 4,718
  • 12
  • 54
  • 81
33
votes
7 answers

Find position of first value greater than X in a vector

I have a vector and want to find the position of the first value that is greater than 100.
curbholes
  • 557
  • 2
  • 5
  • 11
32
votes
7 answers

Finding the Max value in a two dimensional Array

I'm trying to find an elegant way to find the max value in a two-dimensional array. for example for this array: [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] I would like to extract the value '4'. I…
Shuki
  • 323
  • 1
  • 3
  • 5