2017-11-04 96 views
0

我有第一个函数使用!我想在第二个函数中调用succrate和price的乘法,调用初始函数。什么是语法?感谢函数内的函数,数组,Julia

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) 
+0

使你的问题尽可能抽象和清晰,并描述你的确切问题。也许用另一种你知道的语言来显示你想要的东西,比如Java或Python,或者任何其他语言,以便我们知道你要找的语法。 – 7kemZmani

回答

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

+0

好的谢谢。我明白你在说什么。但有没有办法从另一个函数中的函数调用特定的数组?例如我有一个2维数组,它必须从第一个函数调用price [],然后将它与另一个数组相乘。因此,我必须调用价格函数,尤其是价格数组,因为它在乘法运算中用于计算。 – Lvassilopoulos

+0

对不起 - 我不明白你想要做什么...:/ – Liso

+0

例如,如果我想在另一个函数中乘以2d数组的succrate,例如概率[i,d] = succrate [i] ^(d-1)* price [i]我应该如何在当前函数中调用函数参数succrate,price? – Lvassilopoulos