Confusion with eachrow and pipe

Let’s say the rows of A are r1, r2, r3. Then the following have the same meaning:

A |> eachrow |> sum

sum(eachrow(A))

sum([r1, r2, r3])  # Sum of a list of 3 elements (each element being a vector)

r1 + r2 + r3

Your list comprehension does something else… These are equivalent:

[sum(r) for r in eachrow(A)]  # Calculate sum(r) for each row r

[sum(r1), sum(r2), sum(r2)]

Here instead of calculating the sum of three arrays we calculate three sums!

4 Likes