Counting number of 1's in different bins

4 ビュー (過去 30 日間)
MiauMiau
MiauMiau 2015 年 5 月 8 日
コメント済み: MiauMiau 2015 年 5 月 8 日
Hi
I have again a question regarding bins:
Again an array A:
A = [100 150 190 200 250 300 350 370 390 400 400]
and a second array B which saves if a stimuli with the intensity as defined in A was recognized by a subject or not:
B = [ 0 1 0 0 1 1 1 0 1 0 1]
edges = linspace(min(A),max(A),4);
First I bin A in the bins [100,200), [200,300) and [300,400] as follows:
temp = histc(A, edges);
counts = temp(1:end-1);
counts(end) = counts(end)+ temp(end);
Now I want to know for each bin, how many 1's it contains (as stored in B). How do I do that? I started to code with a for loop but the code became quite complicated, so maybe my approach was wrong to start with..
  2 件のコメント
Adam
Adam 2015 年 5 月 8 日
If possible can you post the exact definition of stim_durations and edges for that code?
It is a lot easier to try to help if I (or anyone) can just quickly put your code on our own command line or script without having to interpret it and work out what is supposed to be in a variable.
histc does come with a second output argument:
[bincounts,ind]= histc(___)
ind, being the index of the bin into which each element was placed. Using that to apply to your 'B' array above should quickly yield the answer you need.
As an aside though the Matlab documentation recommends to use histcounts instesad of histc though I don't know which matlab version you are running or which version histcounts was introduced as the recommended alternative.
MiauMiau
MiauMiau 2015 年 5 月 8 日
編集済み: MiauMiau 2015 年 5 月 8 日
I have unfortunately an older Matlab version - and since I have to change the output of histc (see last edge) it does not exactly output the indices I need (thought is still useful I think). I corrected my question now. Thanks though!

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

回答 (3 件)

Michael Haderlein
Michael Haderlein 2015 年 5 月 8 日
編集済み: Michael Haderlein 2015 年 5 月 8 日
histc can have two output parameters:
[temp,ind] = histc(A,[100,200,300,400]);
Use the second one as input parameter for accumarray:
>> accumarray(ind',B',[],@sum)
ans =
1
1
3
1

Purushottama Rao
Purushottama Rao 2015 年 5 月 8 日
For a binary vector B
K= sum(B==1)
will return no of 1s
  1 件のコメント
MiauMiau
MiauMiau 2015 年 5 月 8 日
that is not the question. the question is number of 1s for each bin

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


Andrei Bobrov
Andrei Bobrov 2015 年 5 月 8 日
A = [100 150 190 200 250 300 350 370 390 400 400];
B = [ 0 1 0 0 1 1 1 0 1 0 1];
[~,ii] = histc(A,[100 200 300 inf]);
out = accumarray(ii(:),B(:));

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by