# Eachrow() error?

**URL:** <https://discourse.julialang.org/t/eachrow-error/64516>\
**Category:** New to Julia\
**Created:** [July 12, 2021, 3:48pm UTC](https://discourse.julialang.org/t/eachrow-error/64516 "2021-07-12T15:48:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 12, 2021, 3:48pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/1 "2021-07-12T15:48:13Z")

</div>

```julia
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 “each **row** ” but it should be called “each **col** ” instead ???  
using v1.6.1. Thanks.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 12, 2021, 3:52pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/2 "2021-07-12T15:52:58Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 12, 2021, 4:07pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/3 "2021-07-12T16:07:24Z")

</div>

In fact, the docs for `?eachcol` is _very confusing_:

```julia
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
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.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 12, 2021, 4:11pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/4 "2021-07-12T16:11:40Z")

</div>

Think of `eachrow` and `eachcol` as `Vector`s of `Vector`s. `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 `Vector`s. Julia compares vectors elementwise, so that `[1, 100] > [1, 2]` (I always forget the name for this)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 12, 2021, 4:11pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/5 "2021-07-12T16:11:50Z")

</div>

> [@tomtom](#):
>
> `maximum(x[:, i])`

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 12, 2021, 5:00pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/6 "2021-07-12T17:00:45Z")

</div>

> [@pdeffebach](#):
>
> Maybe part of the confusion is `maximum` on `Vector` of `Vector` s

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.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 12, 2021, 5:01pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/7 "2021-07-12T17:01:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 12, 2021, 5:03pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/8 "2021-07-12T17:03:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 12, 2021, 5:15pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/9 "2021-07-12T17:15:06Z")

</div>

> [@pdeffebach](#):
>
> Feel free to make a PR, then, to improve the docs.

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???

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 12, 2021, 5:27pm UTC](https://discourse.julialang.org/t/eachrow-error/64516/10 "2021-07-12T17:27:12Z")

</div>

You might like this:

> **[Making a first Julia pull request | Katharine Hyatt](https://kshyatt.github.io/post/firstjuliapr/)**
>
> In this post I'm going to go through my step-by-step process of finding some code in base Julia which is not covered by tests, adding tests which do cover it, checking to make sure the tests pass, and finally opening a pull request to have my changes...

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.
