[英]Plot function in octave converting single value to matrix
I am new to octave. 我是八度的新手。 I want to plot lh value for each theta.
我想绘制每个θ的lh值。 I am calculating that lh value using below function.
我正在使用下面的函数计算该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] where theta is generated with theta = 0:0.01:1 D = = [1,1,1,1,1,1,0,0,0,0]其中theta是通过theta = 0:0.01:1生成的
plot(theta,compute_lh(D,theta)) 情节(theta,compute_lh(D,theta))
error: compute_lh: operator *: nonconformant arguments (op1 is 1x101, op2 is 1x101) error: called from compute_lh at line 29 column 10 error: evaluating argument list element number 2 错误:compute_lh:运算符*:不一致的参数(op1为1x101,op2为1x101)错误:在第29行第10列从compute_lh调用错误:评估参数列表元素编号2
I don't know why that theta is converted to matrix while plotting. 我不知道为什么在绘图时将theta转换为矩阵。
The *
operator is matrix multiplication . *
运算符是矩阵乘法 。
Your error occurs because at line lh = lh * theta
, the first time this is called, you're "matrix multiplying" a scalar with a horizontal matrix, resulting in a horizontal matrix. 发生错误的原因是,在第一次调用
lh = lh * theta
时,您是将标量与水平矩阵“矩阵相乘”,从而得到水平矩阵。 The second time, you're trying to "matrix multiply" a horizontal matrix with another horizontal matrix, and that is not a mathematically correct operation, so you get an error. 第二次,您试图将一个水平矩阵与另一个水平矩阵“矩阵相乘”,但这在数学上不是正确的运算,因此会出现错误。
Presumably you need the .*
operator, which is "element-wise" multiplication. 大概需要
.*
运算符,这是“按元素进行”乘法。 Change to that, and you'll see the bell curve result you want. 更改为该名称,您将看到所需的钟形曲线结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.