Eachrow() error?

julia> Random.seed!(1);
julia> x = randn(3, 5);
julia> x
3×5 Matrix{Float64}:
  0.297288  -0.0104452  2.29509   -2.26709   0.583708
  0.382396  -0.839027   0.408396   0.529966  0.963272
 -0.597634   0.311111   0.22901    0.431422  0.458791

julia> [maximum(eachrow(x)) [maximum(x[:, i]) for i in 1:5] ]
5×2 Matrix{Float64}:
  0.382396  0.382396
 -0.839027  0.311111
  0.408396  2.29509
  0.529966  0.529966
  0.963272  0.963272

the above example shows 2 problems:

  1. some of the results not matched ???
  2. it’s called “eachrow” but it should be called “eachcol” instead ???
    using v1.6.1. Thanks.

my misunderstanding. I should use eachcol() with vectorized maximum():

julia> [maximum.(eachcol(x)) [maximum(x[:, i]) for i in 1:5] ]
5×2 Matrix{Float64}:
 0.382396  0.382396
 0.311111  0.311111
 2.29509   2.29509
 0.529966  0.529966
 0.963272  0.963272
1 Like

In fact, the docs for ?eachcol is very confusing:

Example
  ≡≡≡≡≡≡≡≡≡

  julia> a = [1 2; 3 4]
  2×2 Matrix{Int64}:
   1  2
   3  4
  
  julia> first(eachcol(a))
  2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
   1
   3

reading the example above would be confused with the naming of eachrow vs eachcol. It also gives an improper usage !!

actually the example should be provided as:

julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> first.(eachcol(a))
2-element Vector{Int64}:
 1
 2

hope it helps.

Think of eachrow and eachcol as Vectors of Vectors. first(eachcol(a)) just gives the first column. I think the docs are pretty understandable.

Maybe part of the confusion is maximum on Vector of Vectors. Julia compares vectors elementwise, so that [1, 100] > [1, 2] (I always forget the name for this)

1 Like

isn’t that a column? it shouldn’t match what eachrow gives you right?

2×2 Matrix{Int64}:
 1  2
 3  4

1 and 3 form a column, are you from a culture that calls that a row? Eachcol gives you a collection, think of it as [col1, col2], so when you call first(), you’re getting the first column, col1, which is 1 and 3 in the above example.

On the other hand, calling first.() is getting first element of EACH of the columns, of course you get the first row then.

yes. If we replace first() with maximum() in the docs example, my concern would be apparent.

I think at least we should include the use of first.() in the docs example for clarification and comparison.

Feel free to make a PR, then, to improve the docs.

If you add something to the docs, it should probably be map(first, eachcol(a)). Broadcasting over the generator collects it first, which isn’t necessary.

1 Like

I want to do so… but I just don’t know how… it says “You must be on a branch to propose or make changes to this file”… what could I do? How to be “on” a branch???

You might like this:

But for doc PRs, you can also just click the pencil-like button on the github page, and it will (usually) make a fork and make a branch on that, called patch-1 or something.

2 Likes