2017-11-04 100 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