Sum of two vectors of different lengths

moataz-sabry
Sun, 04 Sep 2022 11:47:49 +0000
New to Julia

I’m looking for a short for loop to sum two vectors of different lengths as in the following MWE. In general y could be longer, shorter than x or equal to it in length.

julia> x = [1, 2, 3, 4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> y = [5, 6]
2-element Vector{Int64}:
 5
 6

julia> sum(i + j for (i, j) in (x, y))
14

it stops after considering the two elements of y and returns 14 as result. is there a way to make the loop continue after finishing y, so that the result would be 21?

3 posts - 3 participants