[英]How to define a matrix valued function in Matlab/Octave — return value syntax
[英]Plot function in octave converting single value to matrix
我是八度的新手。 我想繪制每個θ的lh值。 我正在使用下面的函數計算該lh值。
function lh = compute_lh (D, theta)
lh = 1
for i=D
if i == 1
lh = lh * theta
else
lh = lh * (1-theta)
endif
end
endfunction
D = = [1,1,1,1,1,1,0,0,0,0]其中theta是通過theta = 0:0.01:1生成的
情節(theta,compute_lh(D,theta))
錯誤:compute_lh:運算符*:不一致的參數(op1為1x101,op2為1x101)錯誤:在第29行第10列從compute_lh調用錯誤:評估參數列表元素編號2
我不知道為什么在繪圖時將theta轉換為矩陣。
*
運算符是矩陣乘法 。
發生錯誤的原因是,在第一次調用lh = lh * theta
時,您是將標量與水平矩陣“矩陣相乘”,從而得到水平矩陣。 第二次,您試圖將一個水平矩陣與另一個水平矩陣“矩陣相乘”,但這在數學上不是正確的運算,因此會出現錯誤。
大概需要.*
運算符,這是“按元素進行”乘法。 更改為該名稱,您將看到所需的鍾形曲線結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.