# Does Cartesian Indexing work on single values?

**URL:** <https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744>\
**Category:** General Usage\
**Tags:** question, indexing\
**Created:** [December 29, 2021, 1:10am UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744 "2021-12-29T01:10:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 29, 2021, 1:10am UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/1 "2021-12-29T01:10:20Z")

</div>

My goal is to create a function to check if my data points are valid, i.e., not an NaN and not equal to -999.

Here is the function I wrote:  
`f(A) = [idx for idx in CartesianIndices(A) if @inbounds (!isnan(A[idx]) && A[idx] != -999)];`

Why does it work on vector data like N1 but not on single values like N2?

```julia
N1 = [3.7, 4.5];
N2 = 3.7;

```

How do I make it work for single values as well?

Thanks!

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 29, 2021, 2:23am UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/2 "2021-12-29T02:23:03Z")

</div>

There’s no sensible way to assign a Cartesian index to a floating-point value, so Julia doesn’t implement the associated method:

```julia
julia> CartesianIndices(3.7)
ERROR: MethodError: no method matching CartesianIndices(::Float64)
Closest candidates are:
  CartesianIndices(::CartesianIndex) at C:\Users\alexa\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\multidimensional.jl:276
  CartesianIndices(::AbstractArray) at C:\Users\alexa\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\multidimensional.jl:280
  CartesianIndices(::Tuple{}) at C:\Users\alexa\.julia\juliaup\julia-1.7.1+0~x64\share\julia\base\multidimensional.jl:270
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[20]:1

```

You probably want to use the built-in function `findall` instead:

```julia
julia> f(A) = findall(A) do Aᵢ
           !isnan(Aᵢ) && Aᵢ != -999
       end
f (generic function with 1 method)

julia> f([3.7, 4.5])
2-element Vector{Int64}:
 1
 2

julia> f(3.7)
1-element Vector{Int64}:
 1

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 29, 2021, 2:57am UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/3 "2021-12-29T02:57:17Z")

</div>

Thanks for the reply.

That’s so bad. Cartesian index is one of the fastest ways to process large amounts of data.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [December 29, 2021, 3:12am UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/4 "2021-12-29T03:12:53Z")

</div>

You probably want `eachindex` here anyway instead of `CartesianIndices`?

```julia
julia> f(A) = [idx for idx in eachindex(A) if @inbounds (!isnan(A[idx]) && A[idx] != -999)]
f (generic function with 1 method)

julia> N1 = [3.7, 4.5];

julia> N2 = 3.7;

julia> f(N1)
2-element Vector{Int64}:
 1
 2

julia> f(N2)
1-element Vector{Int64}:
 1

```

Or do you require the Cartesian indices in particular?

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 29, 2021, 1:46pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/5 "2021-12-29T13:46:22Z")

</div>

I just realize that it does not work either. Here is why:

```julia
YES(A) = [idx for idx in eachindex(A) if @inbounds (!isnan(A[idx]) && A[idx] != -999)];
N2 = 3.7
Ind = YES(N2)

```

`N2[Ind]` will give me the below error:

```julia
ERROR: MethodError: no method matching getindex(::Float64, ::Vector{Int64})
Closest candidates are:
  getindex(::Number) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:95
  getindex(::Union{AbstractChar, Number}, ::CartesianIndex{0}) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/multidimensional.jl:831
  getindex(::Number, ::Integer) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:96
  ...
Stacktrace:
 [1] top-level scope

```

This seems to be a Julia bug to me. I mean, no indexing system should fail only because the vector is reduced down to just 1 value.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 29, 2021, 3:10pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/6 "2021-12-29T15:10:23Z")

</div>

`N2[Ind...]` to get a scalar index, or encapsulate the scalar in a container: `[N2][Ind]`

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 29, 2021, 3:14pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/7 "2021-12-29T15:14:33Z")

</div>

Do I have to write a loop so that my program will treat a scalar differently from a vector? It seems Julia wants to make something really simple really complex.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 29, 2021, 3:18pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/8 "2021-12-29T15:18:49Z")

</div>

This thread is a bit odd, your scalars should probably be 1-element vectors for type stability, and then CartesianIndex would work too.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 29, 2021, 3:46pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/9 "2021-12-29T15:46:23Z")

</div>

Thanks! The values are generated out of an external program.

Is there a universal function to convert either a scalar, or a 1-column data, or a vector to a vector?

I tried `vec()`, and it does not work for scalars.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 29, 2021, 3:50pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/10 "2021-12-29T15:50:40Z")

</div>

`[x;]` works for both vectors and scalars.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 29, 2021, 3:55pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/11 "2021-12-29T15:55:49Z")

</div>

Excellent! Many thanks

---

<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 29, 2021, 6:36pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/12 "2021-12-29T18:36:25Z")

</div>

> [@stillyslalom](#):
>
> There’s no sensible way to assign a Cartesian index to a floating-point value, so Julia doesn’t implement the associated method:

Sure there is. Julia treats numbers (and characters) as 0-dimensional arrays:

```julia
julia> x = 3.7
3.7

julia> x[]
3.7

julia> x[CartesianIndex()]
3.7

```

so it would make sense to have something like:

```julia
CartesianIndices(::Union{Number,AbstractChar}) = CartesianIndex():CartesianIndex()

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 29, 2021, 7:01pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/13 "2021-12-29T19:01:37Z")

</div>

This gets a bit unintuitive with Julia’s multidimensional `getindex` behavior:

```julia
julia> 1[]
1

julia> 1[CartesianIndex()]
1

julia> 1[1, 1]
1

julia> 1[CartesianIndex(1, 1)]
ERROR: MethodError: no method matching getindex(::Int64, ::CartesianIndex{2})
[...]

```

---

<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 29, 2021, 7:15pm UTC](https://discourse.julialang.org/t/does-cartesian-indexing-work-on-single-values/73744/14 "2021-12-29T19:15:58Z")

</div>

> [@stillyslalom](#):
>
> This gets a bit unintuitive with Julia’s multidimensional `getindex` behavior:

That seems like a separate issue — a missing `getindex` method (for consistency…I’m not sure how useful it is).

Julia has treated numbers as 0-dimensional arrays for years now and we long ago decided to keep it ([make numbers non-iterable? · Issue #7903 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/7903)), so it seems like `CartesianIndices` should be consistent with that…
