Finding index and count of values in an interval
2 ビュー (過去 30 日間)
古いコメントを表示
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)=[]
0 件のコメント
採用された回答
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!