I have two arrays that I want to iterate over at the same time.
I'm using this:
julia> xs = [1,2,3];
julia> ys = [4,5,6];
julia> for i in 1:length(xs)
x = xs[i]
y = ys[i]
@show x, y
end
(x, y) = (1, 4)
(x, y) = (2, 5)
(x, y) = (3, 6)
Is there a better way to iterate over multiple arrays in Julia?
Use zip
along with tuple destructuring:
julia> xs = [1,2,3];
julia> ys = [4,5,6];
julia> for (x, y) in zip(xs, ys)
@show x, y
end
(x, y) = (1, 4)
(x, y) = (2, 5)
(x, y) = (3, 6)
zip
will stop iteration at the shortest array:
julia> for (x, y) in zip([1,2], [0,0,0])
@show x, y
end
(x, y) = (1, 0)
(x, y) = (2, 0)
This pattern can be generalized to an arbitrary number of lists:
julia> for (x, y, z) in zip([1,2], [3,4], [5,6])
@show x, y, z
end
(x, y, z) = (1, 3, 5)
(x, y, z) = (2, 4, 6)
One possibility consists in using the eachindex
function: if it is given multiple Array-like arguments, it will return a iterable set of indices suitable to iterate on all arguments at once.
This is useful in particular in the following situations:
Example 1: using the index itself to fill the first array with values coming from the second
julia> x = [1,2,3];
julia> y = [4,5,6];
julia> @inbounds for i in eachindex(x, y)
x[i] = 2*y[i]
end
julia> x
3-element Array{Int64,1}:
8
10
12
Example 2: check that the arrays have the same range
julia> x = [1,2];
julia> y = [4,5,6];
julia> @inbounds for i in eachindex(x, y)
x[i] = 2*y[i]
end
ERROR: DimensionMismatch("all inputs to eachindex must have the same indices, got [1, 2] and [1, 2, 3]")
Example 3: note that eachindex
generalizes well for multi-dimensional arrays too.
julia> x = zeros(2, 3);
julia> y = ones(2, 3);
julia> @inbounds for i in eachindex(x, y)
x[i] = 2*y[i]
end
julia> x
2×3 Array{Float64,2}:
2.0 2.0 2.0
2.0 2.0 2.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With