Get notified about new tutorials
RECEIVE NEW TUTORIALS

First 15 Minutes Free

302 sessions given

since Jul 09, 2014

since Jul 09, 2014

Likelihood of Reply:
65%

Response Time:
within an hour

Ray Phan

Jun 24, 2015

<p>This goes against my policy in answering questions without any effort made by the question poser, but this seems like an interesting question, so I'll make an exception.</p>
<hr>
<p>First, split up each of the <code>Time</code> and <code>Trial</code> fields so that they're in separate arrays. For the <code>Trial</code> fields, I'm going to convert them into labels 1 and 2 to denote <code>correct</code> and <code>incorrect</code> for ease of implementation:</p>
<pre><code>time = [T.Time].';
trial = {T.Trial}.';
[~,~,trial_ID] = unique(trial);
</code></pre>
<p>Next, what you can do is take each entry in the time array and divide by 100 while taking the floor. Values that belong to the same ID mean that they belong to a group of 100 ms. Note that we also need to add 1 for the next step... you'll see why:</p>
<pre><code>groups = floor(time/100) + 1;
</code></pre>
<p>Now, here's probably one of the most beautiful functions you can ever use in MATLAB: <a href="http://www.mathworks.com/help/matlab/ref/accumarray.html" rel="nofollow"><code>accumarray</code></a>. <code>accumarray</code> groups portions of an array based on an ID and you apply a function to all of the values per group. In our case, we want to group all of the correct and incorrect IDs based on the <code>groups</code> array, then from there we determine the total fraction of values that are incorrect per group. </p>
<p>Specifically, what we're going to do is for each group of values specified in <code>groups</code>, we will take a look at the <code>correct</code> and <code>incorrect</code> numeric labels and determine how many were incorrect by summing over how many were equal to 2 for each group, then dividing by how many there were per group. The groups need to start at index 1, which is why we had to add 1 to <code>groups</code>. Without it, the first group would actually start at 0, and MATLAB starts indexing at 1, hence the offset:</p>
<pre><code>per = accumarray(groups, trial_ID, [], @(x) sum(x == 2) / numel(x));
</code></pre>
<p><code>per</code> contains the fraction that were correct per group, and we get:</p>
<pre><code>>> per
per =
0.5000
0.3333
0.2500
0.6667
</code></pre>
<p>Very nice! Doing a quick hand calculation will demonstrate that you get the correct results.</p>
<hr>
<p>Now the last part is to plot the probabilities on a bar graph. That's very simply:</p>
<pre><code>bar(100*(1:numel(per)), per);
xlabel('Time (ms)');
ylabel('Probability');
</code></pre>
<p>I create a vector that starts from 100 and goes up in multiples of 100 up until as many groups as we have. In our case, we have 4 as the time goes up to 395 ms. </p>
<p>As such, we get:</p>
<p><img src="http://i.stack.imgur.com/0q2mx.png" alt="enter image description here"></p>
<p>This tip was originally posted on <a href="http://stackoverflow.com/questions/30992426/Calculating%20the%20probability%20of%20incorrect%20events%20within%20independent%20groups/30993027">Stack Overflow</a>.</p>