juliamaxargmax# Julia find the index of the maximum element in a list that satisfies another list condition

I want to find the index of the maximum element in a list that satisfy certain conditions.

I have two lists `a = [13, 17, 11, 24, 30]`

and `b = [10, 12, 13, 20, 28]`

. I want to find the index `i`

of the maximum element in `b`

such that `a[i]-b[i]>0`

. The answer here is `i=5`

. Mathematically, I am looking for `argmax_{i in S} b`

where `S = {i : a_i - b_i > 0}`

.

My solution sorts the list and filters it. Is there a better approach?

```
filter(in(findall(>(0), a .- b)), sortperm(b, rev=true))
```

Solution

The following shows a method without a loop. Using a loop should be the easiest go-to solution.
Additionally, benchmarking is demonstrated. Also, the `let`

block shows how to avoid allocations from capturing variables in a closure.

```
julia> a = [13, 17, 11, 24, 30] ; b = [10, 12, 13, 20, 28];
julia> @btime let a = $a, b = $b
findmax(i->a[i]>b[i] ? b[i] : typemin(Int), eachindex(a))
end
5.714 ns (0 allocations: 0 bytes)
(28, 5)
```

For completeness, a benchmark of a `loop`

version:

```
julia> @btime begin
mi = 0 ; m = typemin(Int); for i in eachindex($a)
if $a[i] > $b[i] && $b[i] > m
m = $b[i]
mi = i
end
end; (m, mi)
end
5.495 ns (0 allocations: 0 bytes)
(28, 5)
```

- How to get equally scaled axes with Plots in Julia
- Scatter plot of two rows of a DataFrame in Julia using Plotly
- Adding constraints to jump model from dict
- Non-iterable argument to a function called by Julia `map`?
- Compute row sums and column sums efficiently in Julia
- Julia copying folder into an existing folder
- How to force Julia to use multiple threads for matrix multiplication?
- Can Revise.jl handle `ERROR: LoadError: invalid redefinition of constant`?
- Define piecewise function with automatic broadcasting in Julia
- julia Handling time difference in dataframe
- Fast tensor-dot on sparse arrays with GPU in any programming language?
- Why do allocations occur during broadcasting assignment to a preallocated array?
- Comparing RK4 to Euler method
- How to put even numbers from matrix in separate vector in Julia?
- Number of iterations performed by a for-loop in Julia
- Can I redefine a function, but still use the old definition within the new definition?
- how do I use analytical form as gradient with ! function?
- Julia equivalent to R `as.numeric()`
- Why does Float16(1.1)-Float16(1)=Float16(0.0996)?
- Julia manual and defining an infix operator
- In Julia, how to convert a unsigned number to a signed number like in C?
- Julia Package DataFrames 1.6.1 doesn't recognize old version DataFrames 1.3.4 file
- Convert Julia DataFrame to an array of bytes for compression
- How to suppress svg output by IJulia in Jupyter notebooks
- how to get the documentation of a julia function in a vscode notebook?
- Passing command-line arguments to a Pluto Notebook
- Julia gives "MethodError: rand!" when trying to optimize over a certain manifold using Manopt
- Why doesn't the loss calculated by Flux `withgradient` match what I have calculated?
- How to import Julia packages into Python
- Obtain the number of CPU cores in Julia