How to find the location of the minimum of a vector in Julia
In Julia, an array refers to a multidimensional (n-dimensional) grid of objects.
Arrays are ordered collections of elements allowing duplicate values as opposed to sets. In Julia, arrays are mutable, which means they can be modified or altered after creation.
In Julia, the built-in native function argmin allows us to find out the location of the minimum element of a vector. The index of the first minimal element is returned in case multiple minimal elements exist.
The syntax of this function has many variants:
Syntax | Description |
| This function returns the index of the minimum element of a specified collection |
| This function returns the indices of the minimum elements of array |
Let's look at the following examples.
Example 1
The following example illustrates how to find the location of the minimum element in a single-dimensional vector:
v = [25,35,-1,45,0,-1]println("The index of the minimum is: ", argmin(v))
Explanation
Line 1: Declare a single-dimensional vector containing a collection of values.
Line 2: Invoke the function
argminand display the minimum index.
Example 2
The following example illustrates how to find the location of the minimum element in a multidimensional vector:
v = [[25,35,45],[5,1,25],[1,29,30],[50,36,26],[-1,33,6]]println("The index of the sub-array containing the minimum is: ", argmin(v))subv = v[argmin(v)]println("The sub-array containing the minimum is: ", subv)println("The index of the minimum element in the subarray is: ", argmin(subv))
Explanation
Line 1: Declare a multidimensional vector containing a collection of subarrays.
Line 2: Invoke the function
argminand display the index of the subarray containing the minimum element.Line 3: Initialize a variable
subvwith the subarray containing the minimum element.Line 4: Display the subarray containing the minimum element.
Line 5: Invoke the function
argminand display the index of the minimum element in the picked subarray.
Free Resources