Julia中向量的原位重排? [英] In-place rearrangement of vector in Julia?
本文介绍了Julia中向量的原位重排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在给定索引列表的情况下重新排列向量中的值?
Is it possible to rearrange the values in a vector given a list of indices?
我有两个数组,我想基于arr1
排序arr2
,这两个数组都是预先分配的.
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
推荐答案
permute!
是您的朋友.使用REPL提示符上的?permute!
检查帮助.
permute!
is your friend. Check the help with ?permute!
on the REPL prompt.
具体地说,
permute!(arr2,indices)
应该在OP中就地置换arr2
.但是,文档建议在较大的向量上创建一个新副本可能会更好.
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.
这篇关于Julia中向量的原位重排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文