Skip to content

Any approaches to slice an array with &[[usize]; ndim] ? #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ionizing opened this issue Mar 25, 2022 · 2 comments
Closed

Any approaches to slice an array with &[[usize]; ndim] ? #1159

Ionizing opened this issue Mar 25, 2022 · 2 comments

Comments

@Ionizing
Copy link

Hi, I just came across a problem with the slicing:

Suppose we have an array A of ndim dimensions and &[usize]s containing indices of each dimension, how to operate A with the indices ?

For example, in numpy, we can

A = np.arange(27).reshape(3, 3, 3)
id_dim1 = [0, 2]
id_dim2 = [0, 1]
id_dim3 = [1, 2]

A[id_dim1, id_dim2, id_dim3] *= 2

# produces
A = array([[[ 0,  2,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 46],
        [24, 25, 26]]])

Is it possible to do the same thing within ndarray in an elegant way ?

@Ionizing
Copy link
Author

It seems this question is duplicated with #526 ... but can we do it now?

@Ionizing
Copy link
Author

Ionizing commented Apr 3, 2022

I've tested via julia, this approach is a bit slower than direct loop sum due to the extra allocation. Maybe I should close this issue.

@Ionizing Ionizing closed this as completed Apr 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant