Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-place rearrangement of vector in Julia?

Tags:

julia

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
like image 344
tlnagy Avatar asked Apr 26 '16 21:04

tlnagy


People also ask

How do you write a vector in Julia?

A Vector in Julia can be created with the use of a pre-defined keyword Vector() or by simply writing Vector elements within square brackets([]). There are different ways of creating Vector. vector_name = [value1, value2, value3,..] or vector_name = Vector{Datatype}([value1, value2, value3,..])

How do you get the dot product in Julia?

We should have dot(x::RowVector, y::RowVector) = dot(transpose(x), transpose(y)) (see mailing list). Mathematically, an inner product on a vector space induces an... But if we define dot(a,b) = a'*b one would expect to get back a matrix (the outer product) when a and b are row vectors.

How do you find the magnitude of vector in Julia?

The magnitude of a vector comes from the distance formula applied to a line segment, and is ∥→v∥=√x2+y2 ‖ v → ‖ = x 2 + y 2 . A vector and its unit vector.


1 Answers

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.

like image 96
Dan Getz Avatar answered Sep 23 '22 17:09

Dan Getz