values in falling in each histogram bin

4 views (last 30 days)
zozo
zozo on 21 Aug 2012
Hello,
sig=randn(1,440); %random data set
bin1=round(1+log2(size(sig,2))); %optimal number of bins
edges = linspace(min(sig), max(sig), bin1+1);
bins = (edges(1:end-1) + edges(2:end))/2;
[counts,binIdx] = histc(sig, edges); %bin count
counts(end) = []; %remove the additional added bin
binIdx(binIdx==bin1+1) = bin1; %set the index
figure; hist(sig,bin1) %plot histogram
How do I find the values of sig falling in each bin? My final goal is to have only those values from 'sig' in a variable which are below a count of (say 20) in the histogram.
please help

Answers (1)

José-Luis
José-Luis on 21 Aug 2012
sig=randn(1,440); %random data set
bin1=round(1+log2(size(sig,2))); %optimal number of bins
edges = linspace(min(sig), max(sig), bin1+1);
bins = (edges(1:end-1) + edges(2:end))/2;
[n,xOut]= hist(sig,bins); %or use histc if you want to specify the edges.
n is the vector of values you are looking for.
Cheers!
  7 Comments
José-Luis
José-Luis on 22 Aug 2012
sig=randn(1,440); %random data set
bin1=round(1+log2(size(sig,2))); %optimal number of bins
edges = linspace(min(sig), max(sig), bin1+1);
bins = (edges(1:end-1) + edges(2:end))/2;
PointsInBin = cell(length(edges) - 1,1);
PointsInBin(1) = {sig(sig>= edges(1) & sig<=edges(2))};
for k = 3:length(edges)
PointsInBin(k-1) = {sig(sig>edges(k-1) & sig <= edges(k))};
end
zozo
zozo on 22 Aug 2012
Edited: zozo on 22 Aug 2012
@Jose: Thank you. I have simplified it further:
sig=randn(1,440); %random data set
bin1=round(1+log2(size(sig,2))); %optimal number of bins
edges = linspace(min(sig), max(sig), bin1+1);
bins = (edges(1:end-1) + edges(2:end))/2;
[n,whichbin]= histc(sig,edges);
PointsInBin=cell(length(edges)-1,1);
for k=1:bin1
flagBinMem=(whichbin==k);
PointsInBin(k)={sig(flagBinMem)};
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!