# How do I use isassigned with a Matrix?

**URL:** https://discourse.julialang.org/t/how-do-i-use-isassigned-with-a-matrix/91913
**Category:** New to Julia
**Created:** [December 20, 2022, 8:14pm UTC](https://discourse.julialang.org/t/how-do-i-use-isassigned-with-a-matrix/91913 "2022-12-20T20:14:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![shlemas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shlemas/32/45262_2.png) [@shlemas](https://discourse.julialang.org/u/shlemas)
#### Post date: [December 20, 2022, 8:14pm UTC](https://discourse.julialang.org/t/how-do-i-use-isassigned-with-a-matrix/91913/1 "2022-12-20T20:14:57Z")

</div>

`isassigned` is confusing me a bit.

For simple arrays, it works as I would expect:

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

julia> isassigned(b, 3)
true

julia> isassigned(b, 4) # out of bounds, should return false
false

```

For matrixes, I don’t quite understand what’s going on:

```julia
julia> a = zeros(161, 517)
161×517 Matrix{Float64}:
...

julia> isassigned(a, 500, 514) # out of bounds, should return false...?
true

```

Can someone educate me?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 20, 2022, 8:54pm UTC](https://discourse.julialang.org/t/how-do-i-use-isassigned-with-a-matrix/91913/3 "2022-12-20T20:54:12Z")

</div>

> [@shlemas](#):
>
> `isassigned(a, 500, 514) # out of bounds, should return false...?`

It looks like this is a bug [julia#47942](https://github.com/JuliaLang/julia/issues/47942) in the [`isassigned` code](https://github.com/JuliaLang/julia/blob/8cdb17b48a005a97889f07593c4a619add46ea76/base/array.jl#L219). What it does is to convert `500, 514` to a (zero-based) linear index and check that this is `< length(a)`, which succeeds here because `500-1 + (514-1) * size(a, 1) < length(a)`.

This check ensures that the linear index is in-bounds (so it prevents `jl_array_isassigned` from crashing) but it is not a sufficient check of the multidimensional indices.

Maybe @tim.holy can comment further, because the code originated in [julia#11167](https://github.com/JuliaLang/julia/issues/11167) way back in 2015 (for Julia 0.4).

That being said, if you just want a bounds check (which is all `isassigned` can do for `Matrix{Float64}`), you can call [`checkindex`](https://docs.julialang.org/en/v1/base/arrays/#Base.checkindex) or [`checkbounds`](https://docs.julialang.org/en/v1/base/arrays/#Base.checkbounds):

```julia
julia> checkindex(Bool, axes(a,1), 500) && checkindex(Bool, axes(a,2), 514)
false

julia> checkbounds(a, 500, 514)
ERROR: BoundsError: attempt to access 161×517 Matrix{Float64} at index [500, 514]

```

---

<div class="post-metadata">

### Author: ![shlemas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shlemas/32/45262_2.png) [@shlemas](https://discourse.julialang.org/u/shlemas)
#### Post date: [December 20, 2022, 9:17pm UTC](https://discourse.julialang.org/t/how-do-i-use-isassigned-with-a-matrix/91913/4 "2022-12-20T21:17:51Z")

</div>

Thanks a ton, I missed those check functions. This does exactly what I want.

```julia
julia> checkbounds(Bool, a, 161, 517)
true

julia> checkbounds(Bool, a, 162, 517)
false

```

Of course now I see there is a great page in the docs on this: [Bounds checking · The Julia Language](https://docs.julialang.org/en/v1/devdocs/boundscheck/)

I think I merely happened to stumble on `isassigned` first. The docs for it say it will return false if an index is out of bounds, and it seems to be the most commonly suggested method out there when I google around. So I assumed it was the idiomatic way of doing it. Would be surprising if it has a longstanding issue that nobody hit before me, but I’m sure crazier things have happened.
