为什么for循环在julia中给我带来奇怪的结果?

时间:2018-01-12 13:49:08

标签: for-loop multidimensional-array julia

我在以下嵌套的for循环代码中得到了奇怪的结果。有人能弄明白问题是什么吗?

function FAS(M::Float64,R::Float64,dij::Array{Float64,2},l::Float64, 
         w::Float64,i0::Int64,j0::Int64,pulsing_percent::Float64,β::Float64,
         ρ::Float64,σ::Float64,rad::Float64,k0::Float64,ϕ1::Float64,
         ϕ2::Float64,δ1::Float64,h::Float64)

    M0 = 10^((M+10.7)*(3./2.))*1.0e-7 #Nm 
    nw = size(dij)[1]
    nl = size(dij)[2]
    N = length(dij)
    Δl = l/nl 
    Δw = w/nw
    subfault_radius = sqrt(Δl*Δw/π)
    no_effective_subfaults = nl*pulsing_percent/100
    no_effective_subfaults = no_effective_subfaults/2
    vrup = 0.8*β

    if no_effective_subfaults < 1.
        no_effective_subfaults = 1.
    end     

    #declaration of variables
    dij_sum = sum(dij)
    t_end_max = 0.
    t-arrive_min = 10000.
    risetime_max = 0.0

    #initialization of variables    
    no_active_subfaults = zeros(Int64,nw,nl)
    f0ij = zeros(Float64,nw,nl)
    M0ij = zeros(Float64,nw,nl)
    risetimeij = zeros(Float64,nw,nl)
    subfault_distance = zeros(Float64,nw,nl)
    dur_subij = zeros(Float64,nw,nl)
    delay = zeros(Float64,nw,nl)


    for j in 1:nl
        for i in 1:nw            

        delay[i,j] = i-i0   

        end
    end   
    return delay
end

function main()
    location="F:\\Books\\Thesis\\Paper\\Scripts\\displacements.txt"
    dij = readdlm(location,',')
    M=7.9
    R = 60000.
    l = 195000.
    w = 150000.
    i0 = 6
    j0 = 6
    pulsing_percent = 50.0
    β = 3500.
    ρ = 28000.
    σ = 50*100000.
    rad = 0.55
    k0 = 0.05
    ϕ1 = 293.
    ϕ2 = 120.
    h = 15000.
    δ1 = 7.0
    value = FAS(M,R,dij,l,w,i0,j0,pulsing_percent,β,ρ,σ,rad,k0,ϕ1,ϕ2,δ1,h)
    return value
end

main()

我收到了这个

 10000.0  10000.0  10000.0  10000.0  …  10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0  …  10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0
 10000.0  10000.0  10000.0  10000.0     10000.0  10000.0  10000.0  10000.0

但是,如果我这样做

delay[i,j] = i+(-1*i0)

我得到了

10×13 Array{Float64,2}:
 -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0
 -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0
 -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0
 -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0
  0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
  1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0
  2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0
  3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0
  4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0

这是预期的答案。

有人能弄明白问题是什么吗?

2 个答案:

答案 0 :(得分:1)

您的问题有用户错误,即您认为问题不在于问题。你的大多数例子实际上是不必要的。当我运行生成返回值的代码部分时:

nw = 10
nl = 10
delay = zeros(Float64,nw,nl)
i0 = 6
for j in 1:nl
    for i in 1:nw

    delay[i,j] = i-i0

    end
end

julia> delay
10×10 Array{Float64,2}:
 -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0  -5.0
 -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0  -4.0
 -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0  -3.0
 -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0  -2.0
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0
  0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
  1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0
  2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0   2.0
  3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0   3.0
  4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0   4.0

它会产生您期望的内容,因此此代码不是问题,您需要查看其他地方。

答案 1 :(得分:0)

我弄明白了这个问题。但不知道背后的原因。请弄清楚发生了什么。

function FAS(nw,nl,i0,j0)
    delay = zeros(Float64,nw,nl)
    t-arrive_min= 1000
    for j in 1:nl
        for i in 1:nw

        delay[i,j] = i-i0

        end
    end
    return delay 
end

FAS(10,13,6,6)

现在,如果您打算运行此代码,那么您将获得

10000.0 10000.0 10000.0 10000.0 ... 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 ... 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0  10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0 10000.0

基本上它是一个拼写错误,同时声明t-arri_min = 1000而不是t_arrive_min = 1000.无论运算符“ - ”出现在代码中,它都会为您提供变量设置的值。这是一个非常不寻常的错误,应该报告错误,在声明变量时不允许使用“ - ”运算符。

相关问题