I have the following structure:
T = struct('Time',{20, 40, 50, 80, 120, 150, 190, 210, 250, 260, 270, 320, 350, 380, 385, 390, 395},...
'Trial',{'correct','incorrect','incorrect','correct','correct','correct','incorrect','incorrect','correct','correct','correct','incorrect','incorrect','correct','correct','incorrect','incorrect'});
I would like to perform the following two tasks:
I want to get the probability of having an
'incorrect'
per each 100 ms time window (interval).For example, for the first time window, the first 100 ms, there is 4 trials and 2 are
'incorrect'
out of 4 so it would be2/4 = 0.5
I want to plot a bar graph of the probabilities for each 100 ms time window. The
x
axis would be time and each bar's width would be 100 ms and its height is the probability for that specific window.
I really appreciate any help.
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.
First, split up each of the
Time
andTrial
fields so that they're in separate arrays. For theTrial
fields, I'm going to convert them into labels 1 and 2 to denotecorrect
andincorrect
for ease of implementation: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:
Now, here's probably one of the most beautiful functions you can ever use in MATLAB:
accumarray
.accumarray
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 thegroups
array, then from there we determine the total fraction of values that are incorrect per group.Specifically, what we're going to do is for each group of values specified in
groups
, we will take a look at thecorrect
andincorrect
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 togroups
. Without it, the first group would actually start at 0, and MATLAB starts indexing at 1, hence the offset:per
contains the fraction that were correct per group, and we get:Very nice! Doing a quick hand calculation will demonstrate that you get the correct results.
Now the last part is to plot the probabilities on a bar graph. That's very simply:
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.
As such, we get: