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

I am getting error when runing code :point_down:

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).

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.

for i in 1:N_theta

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


I am getting error in first line of first for loop.

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.

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

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]

I just noticed you are passing in

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

v_r[i, : , :], v_theta[i, :, :] ...
1 Like

Thank You, Please explain more clearly.

most likely the docs can explain more clearly than I can
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]

1 Like