八度音程中的绘图函数将单个值转换为矩阵
Plot function in octave converting single value to matrix
我是八度新手。我想为每个 theta 绘制 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
生成
plot(theta,compute_lh(D,theta))
错误:compute_lh:运算符 *:不一致的参数(op1 是 1x101,op2 是 1x101)
错误:从第 29 行第 10 列的 compute_lh 调用
错误:评估参数列表元素编号 2
我不知道为什么在绘图时将 theta 转换为矩阵。
*
运算符是矩阵乘法。
您的错误发生是因为在第 lh = lh * theta
行,第一次调用此方法时,您是 "matrix multiplying" 具有水平矩阵的标量,结果是水平矩阵。第二次,您试图 "matrix multiply" 一个水平矩阵与另一个水平矩阵,这在数学上不是正确的操作,所以您得到一个错误。
大概您需要 .*
运算符,即 "element-wise" 乘法。
换成那个,你会看到你想要的钟形曲线结果。
我是八度新手。我想为每个 theta 绘制 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
生成plot(theta,compute_lh(D,theta))
错误:compute_lh:运算符 *:不一致的参数(op1 是 1x101,op2 是 1x101) 错误:从第 29 行第 10 列的 compute_lh 调用 错误:评估参数列表元素编号 2
我不知道为什么在绘图时将 theta 转换为矩阵。
*
运算符是矩阵乘法。
您的错误发生是因为在第 lh = lh * theta
行,第一次调用此方法时,您是 "matrix multiplying" 具有水平矩阵的标量,结果是水平矩阵。第二次,您试图 "matrix multiply" 一个水平矩阵与另一个水平矩阵,这在数学上不是正确的操作,所以您得到一个错误。
大概您需要 .*
运算符,即 "element-wise" 乘法。
换成那个,你会看到你想要的钟形曲线结果。