Plot based on bins with binary data

1 回表示 (過去 30 日間)
MiauMiau
MiauMiau 2016 年 11 月 24 日
コメント済み: Alexandra Harkai 2016 年 11 月 25 日
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 件のコメント
Alexandra Harkai
Alexandra Harkai 2016 年 11 月 24 日
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.
MiauMiau
MiauMiau 2016 年 11 月 24 日
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..

サインインしてコメントする。

採用された回答

Alexandra Harkai
Alexandra Harkai 2016 年 11 月 24 日
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 件のコメント
MiauMiau
MiauMiau 2016 年 11 月 25 日
That's the same thing as you posted above though?
Alexandra Harkai
Alexandra Harkai 2016 年 11 月 25 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by