Plot based on bins with binary data

Hello
I have two arrays, say array stimDuration - which contains the duration a subject has seen a stimulation - and array answers, which is binary (containing 1 or 0's) to indicate if subject could correctly identify the stimulus or not. The data could look something like that:
stimDuration = [1, 2, 3, 2, 4, 6, 8, 1, 11, 12, 9] answers = [0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0]
I want now to have n bins for the stimDuration (for instance 2 bins, one going from 1 to 6, the other from 7 to 12) as the x axis. The y axis would be the percentage of the correct answers for each bin. For instance for the bin from 1 to 6, we have a total of 7 answers, 5 of which are correct (hence the y value would be 5/7). Is there a straightforward way of doing that? Thanks

2 Comments

If you have n bins, how would they be distributed exactly? It seems you want them to be of equal size ranging from the smallest to the largest stimDuration values, but it may not be what you ultimately want.
yes and equal size would be ok, as I calculate a percentage, and will have enough trials such that it won't matter if one of the bins contains a bit more data samples..

Sign in to comment.

 Accepted Answer

This would do the trick if you bin them from 0 to max(stimDuration):
n = 2; % number of bins
bar(splitapply(@(x) sum(x)/size(x,2), answers, ceil(stimDuration/(max(stimDuration)/n))));
If you have R2016b you could do:
bar(splitapply(@(x) sum(x)/size(x,2), answers, discretize(stimDuration, n)));

10 Comments

Many thanks! However, I get the following error message: "Undefined fucntion 'splitapply' for input arguments of type 'function_handle'." I do have Matlab 2013b - might that be the problem?
Could be indeed. Does your splitapply help indicate you can pass in a function anywhere?
>>help splitapply
splitapply not found.
Too bad..do you know of any other way I could code it (more or less efficiently) in older Matlab versions?
This may not be helpful since I don't have R2013b to test, but an alternative could be:
bins=ceil(stimDuration/(max(stimDuration)/n));
bar(arrayfun(@(x) sum(answers(x==bins))/size(stimDuration(x==bins), 2), min(bins):max(bins)));
works perfectly - thank you=) And I think I do understand your original question now, since obviously the percentages do not add up to 1, but I think for my question that might be ok! Have a good day
What I meant is: stimDuration goes from 1 to 12. This creates bins to range from 1 to max(stimDuration), but if you had a say a 0 there it won't give you even sizes since that would go to bin 0.
In case stimDuration would have values from 7 to 12, bins would be 1,2, but then all would go to bin 2. Of course that could be changed depending on what you need the bins to be.
This could be a nice general way to divide stimDuration (if it works for R2013b):
bins = discretize(stimDuration, linspace(min(stimDuration), max(stimDuration), n+1));
Hey, I actually just realized that the construction of bins does not work with either methods in my case, due to the range of my data. For instance, this is what I have got in my last measurement for stimDuration:
Columns 1 through 10
0.8500 0.8500 0.8500 0.8870 0.8370 0.8370 0.8370 0.8739 0.8739 0.8239
Columns 11 through 20
0.8239 0.8239 0.8609 0.8609 0.8109 0.7609 0.7109 0.7109 0.7109 0.7479
Usually, the code is putting everything into one and the same bin - and I did not find so far a way to make the binning more sensible..
It is exactly what I meant above: it depends on how you want to decide to split the data to bins.
This should work (again, not sure if R2013b is happy with this):
bins = discretize(stimDuration, linspace(min(stimDuration), max(stimDuration), n+1));
That's the same thing as you posted above though?
Yes. It gives 2 bins as far as I can see. If you look into the documentation for discretize and linspace, you can see what they do and how you can modify the command depending on what you need.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!