简体   繁体   中英

Plot function in octave converting single value to matrix

I am new to octave. I want to plot lh value for each theta. I am calculating that lh value using below function.

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

plot(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

I don't know why that theta is converted to matrix while plotting.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM