# MethodError: no method matching getindex(::Float64, ::Colon, ::Int64)

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727>\
**Category:** Data\
**Tags:** error-message\
**Created:** [March 17, 2024, 4:24pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727 "2024-03-17T16:24:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [March 17, 2024, 4:24pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/1 "2024-03-17T16:24:38Z")

</div>

I am getting error when runing code 👇

```julia
function alfven_surface(N_r, N_theta, theta, v_r, v_theta, B_r, B_theta, rho, tracer)
    """
    Given poloidal velocity field (vp) and poloidal magnetic field (bp), the function
    calculates the Alfven surface. It is the surface for which vp = bp/sqrt{4*pi*rho}
    """
    v_x = zeros(N_r, N_theta)
    v_z = zeros(N_r, N_theta)

	for i in 1:N_theta
        v_x[:, i] .= v_r[:, i] .* sin(theta[i]) .+ v_theta[:, i] .* cos(theta[i])
        v_z[:, i] .= v_r[:, i] .* cos(theta[i]) .- v_theta[:, i] .* sin(theta[i])
    end

    B_x = zeros(N_r, N_theta)
    B_z = zeros(N_r, N_theta)

    for i in 1:N_theta
        B_x[:, i] .= B_r[:, i] .* sin(theta[i]) .+ B_theta[:, i] .* cos(theta[i])
        B_z[:, i] .= B_r[:, i] .* cos(theta[i]) .- B_theta[:, i] .* sin(theta[i])
    end

    vp2 = zeros(N_r, N_theta)
    bp2 = zeros(N_r, N_theta)
    alfsurf = zeros(N_r, N_theta)

    for i in 1:N_theta
        vp2[:, i] .= v_x[:, i] .* v_x[:, i] .+ v_z[:, i] .* v_z[:, i]
        bp2[:, i] .= B_x[:, i] .* B_x[:, i] .+ B_z[:, i] .* B_z[:, i]
        alfsurf[:, i] .= 4.0 * π * rho[:, i] .* vp2[:, i] ./ bp2[:, i]
        if any(alfsurf[:, i] .> 1.0) && any(tracer[:, i] .> 0.1)
            alfsurf[:, i] .= 0.0
        end
    end

    return alfsurf
end
alfsurf = zeros(N_files, N_r, N_theta);
for i in 1:N_files
    alfsurf[i, :, :] = alfven_surface(N_r, N_theta, theta, v_r[i], v_theta[i], B_r[i], B_theta[i], rho[i], tracer[i])
end

```

Here `N_r`=217 and `N_theta`=100.  
Size of `v_r`,` v_theta`, `B_r`, `B_theta`, `rho`, `tracer` are `(1,217,100)`.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 17, 2024, 4:28pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/2 "2024-03-17T16:28:05Z")

</div>

Somewhere you’re doing `array[i, :, j]` and `i` is a `Float64` there, which is not allowed as an index. You can’t use `3.0` for example, only `3`.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [March 17, 2024, 4:32pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/3 "2024-03-17T16:32:41Z")

</div>

```
for i in 1:N_theta

```

`i` will range from 1 to 100 so it will be integer.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/5/85373d02704111322f697f93ca03ea8bbe9f46c5.png)  
I am getting error in first line of first _`for`_ loop.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 17, 2024, 4:34pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/4 "2024-03-17T16:34:22Z")

</div>

Your example is not executable so you cannot expect anyone here to check it in detail for you. The error is clear, so you need to find where you’re picking a floating point number as an index by accident. The `i` in my example was meant as a placeholder, not referring to your code.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [March 17, 2024, 4:47pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/5 "2024-03-17T16:47:13Z")

</div>

likely you are passing in `N_theta` as a float rather than integer

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [March 17, 2024, 4:48pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/6 "2024-03-17T16:48:09Z")

</div>

There is only one index that is `i` and its value lies in range of integer 1 to 100. `N_theta` is Int64.  
`N_theta = readdlm(joinpath(directory, "save_grid.out"), skipstart=N_r+10)[1]`

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [March 17, 2024, 4:49pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/7 "2024-03-17T16:49:00Z")

</div>

I just noticed you are passing in

```julia
v_r[i], v_theta[i], B_r[i], B_theta[i], rho[i], tracer[i]

```

note that these getindex calls will be _scalar_, but I imagined you meant to take a slice? then it should be

```julia
v_r[i, : , :], v_theta[i, :, :] ...

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [March 17, 2024, 4:58pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/8 "2024-03-17T16:58:16Z")

</div>

Thank You, Please explain more clearly.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [March 17, 2024, 5:03pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-float64-colon-int64/111727/9 "2024-03-17T17:03:17Z")

</div>

most likely the docs can explain more clearly than I can  
[https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing)

the idea is that if you index into an array with only a single index like `A[i]`, then it will treat `A` as a flattened object. if you want to index using multiple dimensions that must be explicitly stated like `A[x, y, z]`
