# Mapping along a dimension with findfirst, sometimes failing

**URL:** <https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428>\
**Category:** General Usage\
**Created:** [May 21, 2019, 12:19pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428 "2019-05-21T12:19:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![spaceLem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spacelem/32/217628_2.png) [@spaceLem](https://discourse.julialang.org/u/spaceLem)\
**Post date:** [May 21, 2019, 12:19pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/1 "2019-05-21T12:19:47Z")

</div>

I have a 3d array of records from a model (looking at the spread of disease resistance), the first two dimensions are for some parameter values, the third is for time so `X[1,1,:]` would be how the prevalence of disease resistance changes over time. I’d like a concise way to find the first time that the prevalence passes some critical value (note: it might not reach that value).

I currently have  
`t50 = mapslices(t -> findfirst(x -> x > 0.5, t), X, dims=3)`  
which does `findfirst` for the index of the first time the prevalence passes 0.5.

However, sometimes the prevalence doesn’t reach 0.5, and so I get an error:  
`ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Nothing`  
which I think is because `findfirst` is returning `nothing`. It doesn’t return an array of type `Union{Float64, Nothing}`, which would be what I’d expect.

What I’d ideally like it to do is to specify a default value (e.g. the time of the final index), and just substitute in that, but I’m not sure how to handle the `nothing`s. Right now I’m doing it manually with 3 nested messy `for` loops, but a more elegant solution would be nice, if anyone has any ideas?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 21, 2019, 12:50pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/2 "2019-05-21T12:50:11Z")

</div>

> [@spaceLem](#):
>
> What I’d ideally like it to do is to specify a default value (e.g. the time of the final index)

```julia
mapslices((t -> something(findfirst(x -> x > 0.5, t), default_value), X, dims=3)

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 21, 2019, 12:58pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/3 "2019-05-21T12:58:00Z")

</div>

This seems to be a bug in mapslices: [mapslices errors with type unstable function (problematic when working with Missings) · Issue #26232 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/26232). In the future the solution (other than substituting a default) will probably be a multidimensional `eachslice` (see [Allow type-unstable functions in mapslices by ararslan · Pull Request #31217 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/31217#issuecomment-468782768)).

In the meantime you can substitute a default, or if you do happen to want the `nothing`s you could try

```julia
map(t->findfirst(x->x>0.9, t), (view(X,i,j,:) for i=1:size(X,1), j=1:size(X,2)))

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 21, 2019, 12:59pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/4 "2019-05-21T12:59:45Z")

</div>

`mapslices` unfortunately doesn’t support unions like that. Instead, you can use map over `eachslice`:

```julia
t50 = map(t -> findfirst(x -> x > 0.5, t), eachslice(X, dims=3))

```

---

<div class="post-metadata">

**Author:** ![spaceLem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spacelem/32/217628_2.png) [@spaceLem](https://discourse.julialang.org/u/spaceLem)\
**Post date:** [May 21, 2019, 1:28pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/5 "2019-05-21T13:28:18Z")

</div>

@c42f @mbauman Thanks, both of those look like pretty decent solutions! I didn’t know about `eachslice`, that’s handy to have.

---

<div class="post-metadata">

**Author:** ![spaceLem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spacelem/32/217628_2.png) [@spaceLem](https://discourse.julialang.org/u/spaceLem)\
**Post date:** [May 21, 2019, 4:01pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/6 "2019-05-21T16:01:13Z")

</div>

It looks like my final solution is:

```julia
t50 = map(t -> findfirst(x -> x>0.5, t),
		  (view(X, i,j,:) for i in 1:size(X,1), j in 1:size(X,2))) .|>
	t -> times[something(t), end]

```

which seems pretty readable, and half the length of my loopy based code. `eachslice` would be preferable, but it doesn’t yet support `eachslice(X, dims=(1,2))`, maybe that will be a later version!

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 21, 2019, 11:42pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/7 "2019-05-21T23:42:54Z")

</div>

I think @Tamas_Papp’s solution might work best (be most efficient, and shortest) for your immediate problem where you know what the `nothing` will be turned into.

---

<div class="post-metadata">

**Author:** ![spaceLem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spacelem/32/217628_2.png) [@spaceLem](https://discourse.julialang.org/u/spaceLem)\
**Post date:** [May 22, 2019, 12:43pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/8 "2019-05-22T12:43:42Z")

</div>

Ah, I didn’t realise that `something` was an actual function! Yes, that is a better solution to what I wrote, thank you @Tamas_Papp!

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 22, 2019, 11:33pm UTC](https://discourse.julialang.org/t/mapping-along-a-dimension-with-findfirst-sometimes-failing/24428/9 "2019-05-22T23:33:13Z")

</div>

> [@spaceLem](#):
>
> I didn’t realise that `something` was an actual function

Haha! Yes, it’s somehow oddly named, even though the name makes complete sense once you know what it does.
