Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem with your function is that you have written it assuming that <code>x</code> is a single value, and then you have passed it a vector. Your code cannot cope with this.</p> <p>If you try to run your code with a single value, eg: <code>a=PdfMixUnfmBeta(15,0.4)</code> your function should run.</p> <p>Lets look at what is actually wrong with your code, when I tried running your code I was given the following error: </p> <pre><code>Output argument "y" (and maybe others) not assigned during call </code></pre> <p>This indicates that the lines which assign to <code>y</code> are never actually executed. This indicates that neither of the conditional statements (<code>x &lt; 0</code> and <code>x &gt;= 0</code>) ever evaluate as true. The <code>if</code> statements expects a scalar logical value, but in your example it is provided with a vector of logical. </p> <p>So, to fix this you either need to deal with your <code>x</code> values one at a time by wrapping it in a for loop, e.g.:</p> <pre><code>function y = PdfMixUnfmBeta(x_vec, alpha) for x = x_vec %do your function here end end </code></pre> <p>Alternatively, you can vectorize your code, which is by far the preferable solution:</p> <pre><code>y = zeros(1,length(x)); % where x is &lt; 0 use this formula y(x &lt; 0) = (1-alpha) .*(1/2); % where x is &gt;= 0 use this formula instead. y(x &gt;= 0) = (1-alpha) .* (1/2) + (alpha .* 6 .* x .* (1-x)); </code></pre> <p>In the above solution I use logical indexing and change some of the <code>*</code> to <code>.*</code>. You may find it useful to look up the difference between <code>*</code> and <code>.*</code> and to read up on vectorization.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload