【问题标题】:Plot function in octave converting single value to matrix在八度音阶中绘制函数将单个值转换为矩阵
【发布时间】:2016-07-27 21:33:50
【问题描述】:

我是八度的新手。我想为每个 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 生成的

绘图(theta,compute_lh(D,theta))

错误:compute_lh: operator *: nonconformant arguments (op1 is 1x101, op2 is 1x101) 错误:在第 29 行第 10 列从 compute_lh 调用 错误:评估参数列表元素编号 2

我不知道为什么在绘图时将 theta 转换为矩阵。

【问题讨论】:

  • lh=theta.^sum(D==1).*(1-theta).^(sum(D~=1)) 这样的东西肯定比这更容易吗?即使lh=theta.^nnz(D).*(1-theta).^nnz(~D) D 只有 1 或 0。

标签: matlab octave


【解决方案1】:

* 运算符是矩阵乘法

您的错误发生是因为在第lh = lh * theta 行,第一次调用它时,您将一个标量与一个水平矩阵“矩阵相乘”,从而产生一个水平矩阵。第二次,您尝试将一个水平矩阵与另一个水平矩阵“矩阵相乘”,这在数学上不是正确的运算,因此会出现错误。

大概您需要.* 运算符,它是“逐元素”乘法。 改成那个,你会看到你想要的钟形曲线结果。

【讨论】:

  • @AnipPatel 不,当你调用f(x) 时,对于一个向量x,整个向量都会传入函数中。
猜你喜欢
  • 2013-10-25
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 2011-10-14
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
相关资源
最近更新 更多