julia# Julia: Construct an array from an index array and an array to choose from (or: What is the Julia equivalent of np.choose in numpy?)

In Python (Numpy) there is a np.choose function, that constructs an array from an index array and a list of arrays to choose from.

Simple example (and I am actually mostly interested in this simple version of the problem with a 1D choice-array):

```
import numpy as np
idx_arr = np.array([0, 1, 2, 1, 3])
choices = np.array([0, 10, 20, 30])
new_arr = np.choose(idx_arr, choices) # array([ 0, 10, 20, 10, 30])
```

For the example above the same result can be created in Julia using a for-loop using list comprehension

```
idx_arr = [1, 2, 3, 2, 4];
choices = [0, 10, 20, 30];
new_arr = [choices[idx_arr[i]] for i in 1:length(idx_arr)];
```

Is there a Julia equivalent for `np.choose`

or any other way to achieve this that does not require looping over the index array?

Solution

`choices[idx_arr]`

will work (note that indices are 1-based not 0-based).

The example in the question:

```
julia> idx_arr = [1, 2, 3, 2, 4];
julia> choices = [0, 10, 20, 30];
julia> new_arr = [choices[idx_arr[i]] for i in 1:length(idx_arr)]
5-element Vector{Int64}:
0
10
20
10
30
julia> choices[idx_arr]
5-element Vector{Int64}:
0
10
20
10
30
```

- 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