Note that there are some explanatory texts on larger screens.

plurals
  1. POImprove performance of nested loops MATLAB
    text
    copied!<p>I am trying to analyse a lot of data which is making my program run painfully slow. I am reading in a dataset from a .txt file to a cell array. I am using a cell array as in order to classify my data, which is in the form of two attributes, I need the classes which are chars. </p> <p>I want to find the resubstitution error using a nearest mean classifier. I have a main outer loop which loops through each line of my dataset (tens of thousands). Each line is removed in turn, one on each iteration. The mean of two attributes is re calculated on each iteration with the line removed. The main hang point seems to be the next section where I want to calculate for each line in my dataset:</p> <ul> <li>The Euclidian distance between the data on that line (2 attribute values) and the mean of each of my classes. </li> <li>I then want to record the class whose mean the attributes mean value is closest to, this will be its assigned class. </li> <li>Finally i want to check whether this assigned class is the correct class.</li> </ul> <p>Currently this loop looks like this.</p> <pre><code>errorCount = 0; for l = 1:20000 closest = 100; class = 0; attribute1 = d{2}(l); attribute2 = d{3}(l); for m = 1:numel(classes) dist = sqrt((attribute1-meansattr1(m))*(attribute1-meansattr1(m)) + (attribute2-meansattr2(m))*(attribute2-meansattr2(m))); if dist &lt; closest closest = dist; class = m; end end if strcmp(d{1}(l),classes(class)) %correct else errorCount = errorCount + 1; end end </code></pre> <p><code>d</code> is my cell array where <code>d{2}</code> is a column holding my attribute 1 values. I access these values with <code>d{1}(1)</code> for the first row in that column.</p> <p><code>classes</code> are the unique classes from my dataset, so for each of my classes I calculate the Euclidean distance to it.</p> <p><code>meansattr1</code> and <code>meansattr2</code> are arrays containing the mean values for each of my attributes. These are updated on each iteration of the outer loop when a line is removed.</p> <p>Hopefully that helps you understand the code that I have. Any help in optimising and speeding up these calculations is much appreciated. </p>
 

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