Finding index and count of values in an interval

조회 수: 3 (최근 30일)
Jared
Jared 2013년 4월 9일
I have 2 vectors that I am comparing. I am using 1 as the "bin edges" and the other one is the data I want a count of. Histc seems to be the way to go for this, and does give me the count I want.
However, I also want to find the index of the first value in each bin, so I can use the actual data value for other calculations.
Example: There are 4 bins, 1-2, 2-3, 3-4, 4-5
x=[1 2 3 4 5];
y=[.8 .9 1 1.2 1.8 2.1 3.1 3.2 5 6 7 8 9 10];
n_elements=histc(y,x);
n_elements(length(n_elements))=[];
n_elements=[3 1 2 0]
The problem I am having is I need a way to find the index of the first y value in each bin. I want something like: y_index(1)=3, y_index(2)=6, y_index(3)=7, y_index(4)=[]

채택된 답변

Thorsten
Thorsten 2013년 4월 9일
This should do the job
x = [1 2 3 4 5];
for i = 1:numel(x)-1
[val ind_firstentry(i)] = min(hwrect(x(i)-y));
if y(ind_firstentry(i)) >= x(i+1)
ind_firstentry(i) = []; % remove entry out of bounds
end
end
disp(num2str(ind_firstentry))
With an additional little helper function
function x = hwrect(x)
%HWRECT Half wave rectification: negative elements are set to zeros.
% HWRECT(X) = max(x, 0).
x(find(x < 0)) = 0;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by