selectperm
selectperm(v, k, [alg=
Return a partial permutation of the the vector v
, according to the order specified by by
, lt
and rev
, so that v[output]
returns the first k
(or range of adjacent values if k
is a range) values of a fully sorted version of v
. If k
is a single index (Integer), an array of the first k
indices is returned; if k
is a range, an array of those indices is returned. Note that the handling of integer values for k
is different from select
in that it returns a vector of k
elements instead of just the k
th element. Also note that this is equivalent to, but more efficient than, calling sortperm(...)[k]
Examples
julia> v = [5, 2, 8, 3, 1];
julia> selectperm(v, 3)
3-element Array{Int64,1}:
5
2
8
In this example, the selectperm
function is used to obtain a partial permutation of the vector v
, where the first 3 elements of a fully sorted version of v
are selected.
julia> v = [5, 2, 8, 3, 1];
julia> selectperm(v, 2:4)
3-element Array{Int64,1}:
2
3
1
This example demonstrates the usage of selectperm
with a range 2:4
as the second argument. It returns an array of indices corresponding to the elements in v
that would be selected if the range 2:4
were applied to a fully sorted version of v
.
julia> v = [5, 2, 8, 3, 1];
julia> selectperm(v, 2, rev=true)
2-element Array{Int64,1}:
8
5
In this example, the rev=true
argument is used to obtain a partial permutation of the vector v
in reverse order. The first 2 elements of a fully sorted version of v
are selected and returned in reverse order.
julia> v = [5, 2, 8, 3, 1];
julia> selectperm(v, 1, by=abs)
1-element Array{Int64,1}:
1
Here, the selectperm
function is used with the by=abs
argument to obtain a partial permutation of the vector v
where the element with the smallest absolute value is selected.
julia> v = [5, 2, 8, 3, 1];
julia> selectperm(v, 1, lt=>, by=x->-x)
1-element Array{Int64,1}:
8
In this example, the selectperm
function is used with the lt=>
argument to obtain a partial permutation of the vector v
where the element with the largest value is selected.
julia> v = [5, 2, 8, 3, 1];
julia> selectperm(v, 1, alg=QuickSort)
1-element Array{Int64,1}:
1
This example showcases the usage of selectperm
with the alg=QuickSort
argument to obtain a partial permutation of the vector v
using the QuickSort algorithm. The element with the smallest value is selected.
See Also
complement, complement!, intersect, intersect!, issubset, selectperm, selectperm!, Set, setdiff, setdiff!, symdiff, union, union!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.