函数内的函数,数组,Julia

时间:2017-11-04 05:57:07

标签: julia

我有第一个使用!的函数,我希望在第二个函数中得到succrate和price的乘法,调用初始函数。语法是什么?谢谢

length_of_arrays = 101

lower_limit = 0
steps_per_unit = 1

price1 = 10

succrate1 = 5
succrate2 = 7

price = Array{Float64, 1}(101)
succrate = Array{Float64, 2}(101,20)    


function modifyarrays!(length_of_arrays, price, lower_limit, steps_per_unit, succrate)
for pr_A in 1:101

price[pr_A] = lower_limit + ((pr_A-1) / steps_per_unit)

  for d in 1:20
    if price[pr_A] == price1
        succrate[pr_A, d] = succrate1
    else
        succrate[pr_A, d] = succrate2
    end
  end
 end

end

modifyarrays!(101, price, 0, 1, succrate)

1 个答案:

答案 0 :(得分:1)

function set_price_at!(price, pr_A, lower_limit, steps_per_unit) 
    price[pr_A] = lower_limit + ((pr_A-1) / steps_per_unit)
    nothing
end

function set_succrate_at!(succrate, pr_A, price, succrate1, succrate2)
    set_price_at!(price, pr_A, lower_limit, steps_per_unit) # You could call it here (1)
    for d in 1:20
        if price[pr_A] == price1
            succrate[pr_A, d] = succrate1
        else
            succrate[pr_A, d] = succrate2
        end
    end
end

function modifyarrays!(length_of_arrays, price, lower_limit, steps_per_unit, succrate)
    for pr_A in 1:101
        # set_price_at!(price, pr_A, lower_limit, steps_per_unit) # or here (2)
        set_succrate_at!(succrate, pr_A, price, succrate1, succrate2)
    end
end

price = rand(Float64, (101,))
succrate = rand(Float64, (101,20))

modifyarrays!(101, price, 0, 1, succrate)

我喜欢在(2)调用函数而不是在(1)调用它。