Find the Maximum Value in an Array in Julia

Find the Maximum Value in an Array in Julia

  • Julia
  • 2 mins read

In Julia, you can use the maximum() function from the Base package to find the maximum value in an array.

Here is an example:

Find Max Value in an Array in Julia

using Base

# Define an array
arr = [1, 2, 3, 4, 5]

# Find the maximum value in the array
max = maximum(arr)

# Print the result
println(max)

In this example, the Base package is imported, and then an array is defined using the Array type. The maximum() function is then used to find the maximum value in the array. The maximum() function takes a single argument: the array to find the maximum value in.

The output of this code would be:

5

This is the maximum value in the array.

You can also specify the dimension along which to find the maximum value by passing the dims keyword argument to the maximum() function, like this:

using Base

# Define a 2D array
arr = [[1, 2, 3],
       [4, 5, 6]]

# Find the maximum value in each row of the array
max = maximum(arr, dims = 1)

# Print the result
println(max)

In this example, the maximum() function is called with the dims keyword argument set to 1, which specifies that the maximum value should be found in each row of the array. The maximum() function returns an array with the maximum value from each row of the input array.

The output of this code would be:

1-element Vector{Vector{Int64}}:
 [4, 5, 6]

This is an array containing the maximum value from each row of the input array.

Related:

  1. Check if Array is Empty in Julia
  2. Check if all Elements in Array are Equal in Julia