This question already has answers here:
Index Array without Elements
(3 answers)
Closed 2 years ago.
Say x = [1:5..] and I wish to return an array with the element 1:2 and 4:5 i.e. all but one element namely 3. How do I do that?
I tried x[1:2; 4:end] and x[1:2 4:end]. Neither worked.
I would really like to use the end keyword if possible.
InvertedIndices.jl has a nice interface for this:
julia> using InvertedIndices
julia> v = map(i -> i => rand(), 1:5)
5-element Array{Pair{Int64,Float64},1}:
1 => 0.8165266824627073
2 => 0.38840874144349025
3 => 0.061178225310028145
4 => 0.6615139442678073
5 => 0.10733363621427094
julia> v[Not(3)]
4-element Array{Pair{Int64,Float64},1}:
1 => 0.8165266824627073
2 => 0.38840874144349025
4 => 0.6615139442678073
5 => 0.10733363621427094
You could do a union of the indices:
julia> x = [1:5..]
5-element Array{Int64,1}:
1
2
3
4
5
julia> x[(1:2) ∪ (4:end) ]
4-element Array{Int64,1}:
1
2
4
5
I typed the "union" symbol by writing \cup and hitting TAB
Related
How can I convert a Set to an Array in Julia?
E.g. I want to transform the following Set to an Array.
x = Set([1,2,3])
x
Set{Int64} with 3 elements:
2
3
1
The collect() function can be used for this. E.g.
collect(x)
3-element Vector{Int64}:
2
3
1
Notice, however, that the order of the elements has changed. This is because sets are unordered.
You can also use the splat operator on sets:
julia> [x...]
3-element Vector{Int64}:
2
3
1
However, this is slower than collect.
You can use [ ] to create an array.
x = Set([1,2,3])
y = [a for a in x]
y
2
3
1
typeof(y)
Vector{Int64} (alias for Array{Int64, 1})
You can use a comprehension:
x = Set(1:5)
#time y = [i for i in x]
> 0.000006 seconds (2 allocations: 112 bytes)
typeof(y)
> Vector{Int64} (alias for Array{Int64, 1})
I am using Julia1.6
Here, X is a D-order multidimeonal array.
How can I slice from i to j on the d-th axis of X ?
Here is an exapmle in case of D=6 and d=4.
X = rand(3,5,6,6,5,6)
Y = X[:,:,:,i:j,:,:]
i and j are given values which are smaller than 6 in the above example.
You can use the built-in function selectdim
help?> selectdim
search: selectdim
selectdim(A, d::Integer, i)
Return a view of all the data of A where the index for dimension d equals i.
Equivalent to view(A,:,:,...,i,:,:,...) where i is in position d.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> A = [1 2 3 4; 5 6 7 8]
2×4 Matrix{Int64}:
1 2 3 4
5 6 7 8
julia> selectdim(A, 2, 3)
2-element view(::Matrix{Int64}, :, 3) with eltype Int64:
3
7
Which would be used something like:
julia> a = rand(10,10,10,10);
julia> selectedaxis = 5
5
julia> indices = 1:2
1:2
julia> selectdim(a,selectedaxis,indices)
Notice that in the documentation example, i is an integer, but you can use ranges of the form i:j as well.
If you need to just slice on a single axis, use the built in selectdim(A, dim, index), e.g., selectdim(X, 4, i:j).
If you need to slice more than one axis at a time, you can build the array that indexes the array by first creating an array of all Colons and then filling in the specified dimensions with the specified indices.
function selectdims(A, dims, indices)
indexer = repeat(Any[:], ndims(A))
for (dim, index) in zip(dims, indices)
indexer[dim] = index
end
return A[indexer...]
end
idx = ntuple( l -> l==d ? (i:j) : (:), D)
Y = X[idx...]
How can i use counts() to show the frequencies and the items? for example:
a=[1,2,2,3]
count(a) gives 1,2,1
How can i do to get:
1:1, 2:2, 3:1?
Thanks
It looks like you are already using StatsBase, because that is where the counts function you mention is defined. The function you are looking for is called countmap:
using StatsBase
a = [1,2,2,3];
countmap(a)
# Dict{Int64, Int64} with 3 entries:
# 2 => 2
# 3 => 1
# 1 => 1
If you prefer tabular output you can also do:
julia> using FreqTables
julia> a = [1,2,2,3];
julia> freqtable(a)
3-element Named Vector{Int64}
Dim1 │
──────┼──
1 │ 1
2 │ 2
3 │ 1
I am new in Julia. I want to do a program where I can use group Actions.
For example: Take a vector $(a,b,c,d)$ in $\mathbb{R}^{4}$, and consider the action of elements of $S_{4}$ on this, such as the cycle $(1,2,3,4)$. I would like to have a program which computes:
$$(1,2,3,4)(a,b,c,d) = (d,a,b,c)$$
It would be great if this would be possible for any permutation. Do you have any ideas which packages must I download? and How must I write it?
thank you for your help.
You might like the package Permutations, https://github.com/scheinerman/Permutations.jl.
julia> using Permutations
julia> p = Permutation([2,3,4,1]) # an element of S_4
(1,2,3,4) # printed in cycle notation
julia> p(1) # apply it to one element
2
julia> two_row(p) # alternative way to print this
2×4 Matrix{Int64}:
1 2 3 4
2 3 4 1
julia> inv(p)
(1,4,3,2)
julia> input = [:a, :b, :c, :d]; # an array of any 4 things
julia> [input[p(i)] for i in eachindex(input)] # permute those?
4-element Vector{Symbol}:
:b
:c
:d
:a
julia> ans == input[p.(eachindex(input))] # another way to write the same
true
julia> input[inv(p).(eachindex(input))] # with inv(p)
4-element Vector{Symbol}:
:d
:a
:b
:c
julia> Permutation([2,3,1,4,6,5])
(1,2,3)(4)(5,6) # it really prints the cycles
Does Julia have a build in command to find the index of the minimum of a vector? R, for example, has a which.min command (and a which.max, of course).
Obviously, I could write the following myself, but it would be nice not to have to.
function whichmin( x::Vector )
i = 1
min_x=minimum(x)
while( x[i] > min_x )
i+=1
end
return i
end
Apologies if this has been asked before, but I couldn't find it. Thanks!
Since 0.7-alpha, indmin and indmax are deprecated.
Use argmin and argmax instead.
For a vector it just returns the linear index
julia> x = rand(1:9, 4)
4-element Array{Int64,1}:
9
5
8
5
julia> argmin(x)
2
julia> argmax(x)
1
If looking for both the index and the value, use findmin and findmax.
For multidimensional array, all these functions return the CartesianIndex.
I believe indmax(itr) does what you want. From the julia documentation:
indmax(itr) → Integer
Returns the index of the maximum element in a collection.
And here's an example of it in use:
julia> x = [8, -4, 3.5]
julia> indmax(x)
1
There's also findmax, that returns both the maximum value and its position.
For multidim array, you'll have to switch between linear indexes et multidim indexes:
x = rand(1:9, 2,3)
# 2×3 Array{Int64,2}:
# 5 1 9
# 3 3 8
indmin(x)
# 3
# => third element in the column-major ordered array (value=1)
ind2sub(size(x),indmin(x))
# (1, 2)
# => (row,col) indexes: what you are looking for.
-- Maurice