Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're building the matrix <code>output</code> from scratch, you should make sure it hasn't already been initialized to anything. To do this, you can set it to the empty matrix at the beginning of your code:</p> <pre><code>output = []; </code></pre> <p>Also, if you know how large <code>output</code> is going to be, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-784135.html#f8-793781" rel="nofollow noreferrer">your code will run more efficiently if you preallocate the array</a> <code>output</code> and index into the array to assign values instead of appending values to it. In your case, <code>output</code> should have the same number of rows as there are unique values in the array <code>ordered1</code>, so you could use the function <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/unique.html" rel="nofollow noreferrer">UNIQUE</a> to preallocate <code>output</code>:</p> <pre><code>nRows = numel(unique(ordered1)); %# Get the number of unique values output = zeros(nRows,2); %# Initialize output </code></pre> <p>You would then have to keep a separate counter (say <code>r</code>) to track which index into <code>output</code> you will be adding to next:</p> <pre><code>... output(r,:) = [count/537 tempnum]; %# Overwrite a row in output r = r+1; %# Increment the row index ... </code></pre> <h2>Some additional advice...</h2> <p>Even if you solve the error you are getting, you're going to run into more with the code you have above:</p> <ul> <li><p>I believe you are actually computing a <a href="http://en.wikipedia.org/wiki/Probability_density_function" rel="nofollow noreferrer">probability density function</a> (or PDF) with your code. In order to get the <a href="http://en.wikipedia.org/wiki/Cumulative_distribution_function" rel="nofollow noreferrer">cumulative distribution function</a> (or CDF), you have to perform a cumulative sum over the final values in the first column of <code>output</code>. You can do this with the function <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cumsum.html" rel="nofollow noreferrer">CUMSUM</a>:</p> <pre><code>output(:,1) = cumsum(output(:,1)); </code></pre></li> <li><p>Your loop will throw an error when it reaches the last element of <code>ordered1</code>. The value of <code>k</code> can become 538 in your inner while loop, which will then cause an error to be thrown when you try to access <code>ordered1(k)</code> anywhere. To get around this, you will have to add checks to the value of <code>k</code> at a number of points in your code. One such point is your inner while loop, which can be rewritten as:</p> <pre><code>while (k &lt;= 537) &amp;&amp; (ordered1(k) == tempnum) count = count + 1; k = k + 1; end </code></pre> <p>This solution uses the <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/logicaloperatorsshortcircuit.html" rel="nofollow noreferrer">short-circuit AND operator</a> <code>&amp;&amp;</code>, which will first check if <code>(k &lt;= 537)</code> is true or false. If it is false (i.e. <code>k &gt; 537</code>), the second logical check is skipped since its result doesn't matter, and you avoid the error that would result from evaluating <code>ordered1(k)</code>.</p></li> </ul> <h2>Bonus MATLAB coolness...</h2> <p>MATLAB has a lot of cool functions that can do a lot of the work for you. One such function is <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/accumarray.html" rel="nofollow noreferrer">ACCUMARRAY</a>. Your TA may want you to do things using loops like you have above, but you can actually reduce your whole code to just a few lines like so:</p> <pre><code>nValues = numel(ordered1); %# Get the number of values p = accumarray(ordered1,ones(size(ordered1)))./nValues; %# Create a PDF output = [cumsum(p) unique(ordered1)]; %# Create the CDF output </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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