Finding index and count of values in an interval
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Thorsten
el 9 de Abr. de 2013
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;
Más respuestas (0)
Ver también
Categorías
Más información sobre Holidays / Seasons en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!