Counting number of 1's in different bins

조회 수: 3(최근 30일)
MiauMiau 2015년 5월 8일
댓글: MiauMiau 2015년 5월 8일
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
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!

댓글을 달려면 로그인하십시오.


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 =

Purushottama Rao
Purushottama Rao 2015년 5월 8일
For a binary vector B
K= sum(B==1)
will return no of 1s
  댓글 수: 1
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(:));

Community Treasure Hunt

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

Start Hunting!

Translated by