In-place rearrangement of vector in Julia?

2019-06-21 13:04发布

Is it possible to rearrange the values in a vector given a list of indices?

I have two arrays and I want to sort arr2 based on arr1 which are both preallocated.

indices = zeros(length(arr1))
sortperm!(indices, arr1)
arr2[indices] <-- this returns a copy

标签: julia
1条回答
我想做一个坏孩纸
2楼-- · 2019-06-21 13:53

permute! is your friend. Check the help with ?permute! on the REPL prompt.

Specifically,

permute!(arr2,indices)

should permute in-place arr2 in the OP. But, the docs suggest on large vectors it might be better to just create a new copy.

查看更多
登录 后发表回答