Find the tail of array except first element - julia

Is there a method to get the tail of a collection(e.g. vector) i.e. the array except the first element. Currently, I have
tail(arr) = selectdim(arr, 1, 2:length(arr))
julia> c
3-element Array{Int64,1}:
1
2
3
julia> tail(c)
2-element view(::Array{Int64,1}, 2:3) with eltype Int64:
2
3

Simply use the end keyword:
julia> c = [11,12,13]
3-element Array{Int64,1}:
11
12
13
julia> c[2:end]
2-element Array{Int64,1}:
12
13

Related

How can I determine if an array contains some element?

How can I tell if an array contains some element?
I have been manually checking with a loop:
for x in xs
if x == a
return true
end
end
return false
Is there a more idiomatic way?
The in operator will iterate over an array and check if some element exists:
julia> xs = [5, 9, 2, 3, 3, 8, 7]
julia> 8 in xs
true
julia> 1 in xs
false
It is important to remember that missing values can alter the behavior you might otherwise expect:
julia> 2 in [1, missing]
missing
in can be used on general collections. In particular, matrices:
julia> A = [1 4 7
2 5 8
3 6 9]
3×3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
julia> 7 in A
true
julia> 10 in A
false

How to select odd numbers from a matrix?

How do you select odd numbers from a matrix larger than 29?
You can use filter:
filter(x->isodd(x)&&x>29, M)
Here, x->isodd(x)&&x>29 is an anonymous function, specifying your filter criterium, and M is your matrix.
Example:
julia> M = rand(1:50, 3,3)
3×3 Array{Int64,2}:
20 42 35
23 6 31
28 4 4
julia> filter(x->isodd(x)&&x>29, M)
2-element Array{Int64,1}:
35
31
Alternatively, you can use array comprehensions:
julia> [x for x in M if isodd(x) && x>29]
2-element Array{Int64,1}:
35
31
You can also use this:
using Statistics;
A = round.(rand(5,5)*10)
odd = A[(rem.(A,2).>0) .& (A.>29)]

newline sensitive interpretation of Arrays

When I add a newline to my array definition, the type of my array changes.
julia> a = [[1]]
1-element Array{Array{Int64,1},1}:
[1]
julia> a = [[1]
]
1-element Array{Int64,1}:
1
I thought they both should return the same result i.e. of type Array{Array{Int64,1},1}
In order to understand this see the following:
julia> :([[1]
])
:([[1];])
And you see that adding a newline is rewritten as vcat operation.
The reason for this is to allow writing something like this:
julia> x = [1 2
3 4]
2×2 Array{Int64,2}:
1 2
3 4
and your example is hitting a corner case of this syntax.
Note, however, that without an extra empty line vcat is not called:
julia> :([[1]
])
:([[1]])
Another use-case that is worth to know is:
julia> [[1, 2]
[3, 4]]
4-element Array{Int64,1}:
1
2
3
4
and the same with variables (can improve code readability in some cases):
julia> a = [1,2]
2-element Array{Int64,1}:
1
2
julia> b = [3, 4]
2-element Array{Int64,1}:
3
4
julia> [a
b]
4-element Array{Int64,1}:
1
2
3
4

How to apply one argument to Array{Function, 1} element-wise smartly in Julia?

I understand Julia can apply element-wise argument to a function by f.(x)
in v0.6
x = [1, 2, 3]
f = x->3x
#assert f.(x) = [3, 6, 9]
Now, I define f as Array{Function, 1}.
f1(x) = 3x
f2(x) = 4x
f = [f1, f2]
x = 2
#assert isa(f, Array{Function,1}) == true
# f.(x) is unavailable
I want to apply arguments to element-wise function like above syntax, not using map, [_f(x) for _f in f]
Can someone be familiar with this issue?
You can broadcast the pipe operator (|>), e.g.
x .|> f
You have to take care how to define x and f, in some cases you will fail. Your first x applied on your last f will fail.
julia> 5 .|> [x->2x, x->7x]
2-element Array{Int64,1}:
10
35
julia> [2, 5] .|> [x->2x, x->7x]
2-element Array{Int64,1}:
4
35
julia> [2 5] .|> [x->2x, x->7x]
2-element Array{Int64,2}:
4 10
14 35
julia> [2 5] .|> [x->2x x->7x]
1×2 Array{Int64,2}:
4 35
julia> [2, 5] .|> [x->2x x->7x]
2×2 Array{Int64,2}:
4 14
10 35
julia> [2 3 5] .|> [x->2x x->7x]
ERROR: DimensionMismatch("arrays could not be broadcast to a common")
julia> [2, 3, 5] .|> [x->2x, x->7x]
ERROR: DimensionMismatch("arrays could not be broadcast to a common")
julia> x = [1, 2, 3]; f = [x->2x, x->3x]; x .|> f
ERROR: DimensionMismatch("arrays could not be broadcast to a common")
Here's another possibility:
((f, x)->f(x)).([f1, f2], x)

Equivalent of the dot prefix for an arbitrary function?

Julia allows me to do elementwise binary operators on arrays/matrices with .:
[1 2 3] .+ [3 2 1]
[1 2 3] .> [3 2 1]
Is there a standard way to apply an arbitrary function f elementwise to a pair of arrays?
If you just want to apply a binary function to each pair of values from two lists, you can use map:
f(x,y) = 2x - y^2
julia> map(f, [1, 2, 3], [3, 2, 1])
3-element Array{Int64,1}:
-7
0
5
The .+ and .> operators also have the additional behavior that singleton dimensions are broadcast, like so:
julia> [1, 2, 3] .+ [-1 -2 -3]
3x3 Array{Int64,2}:
0 -1 -2
1 0 -1
2 1 0
julia> rand(3,4) .+ [1, 2, 3]
3x4 Array{Float64,2}:
1.73798 1.84132 1.12923 1.30192
2.10961 2.17835 2.52779 2.3028
3.16457 3.04659 3.67604 3.08869
julia> rand(3,4) .+ [1 2 3 4]
3x4 Array{Float64,2}:
1.40294 2.31384 3.34001 4.60027
1.13289 2.99275 3.50606 4.51049
1.31486 2.7585 3.64655 4.59647
If you also want this kind of behavior, you can use the broadcast function:
julia> broadcast(f, [1 2 3], [3, 2, 1])
3x3 Array{Int64,2}:
-7 -5 -3
-2 0 2
1 3 5

Resources