function c = counts(x,v) % Copyright (c) 2005-2009 by Ales Cerny %-------------------------------------------------------------------------- % counts(x,v) counts the numbers of elements of vector x that fall into % specified ranges determined by vector v. % % Inputs % x : N*1 vector containing the numbers to be counted. % v : P*1 vector containing breakpoints specifying the ranges within % which counts are to be made. v must be sorted in ascending % order. % % Outputs % c : P*1 vector, the counts of the elements of x that fall into the % regions: % x <= v(1), % v(1) < x <= v(2), % : % : % v(p-1) < x <= v(p). %-------------------------------------------------------------------------- %************************************************************************% % counts.m - supplementary program to % % Ales Cerny (2009) Mathematical Techniques in Finance (2nd ed.) % % Princeton University Press http://press.princeton.edu/titles/9079.html % %************************************************************************% % This code is provided 'as-is', without any express or implied warranty. % % Permission is granted to anyone to use this code for any purpose, % subject to the following restrictions: % % 1. The origin of this code must not be misrepresented; you must not % claim that you wrote the original code. % 2. Modified code versions must be plainly marked as such, and must not % be misrepresented as being the original code. % 3. This notice may not be removed from any source distribution. % NOTICE TO STUDENTS: To avoid accusations of plagiarism, if you use this % code or its modifications in assessed work you should prepend it with a % note stating: % "This is the original/modified version of the code counts.m by % Ales Cerny (2009), Mathematical Techniques in Finance (2nd ed.), % Princeton University Press. The original version is available from % http://www.martingales.info/mtfweb2". % A similar acknowledgement should appear prominently inside your written % report. if nargin < 2 error('Requires at least two input argument.'); end k = find(v(2:end) - v(1:end-1) <=0); if any(k) error('v must be sorted in ascending order.'); end temp = zeros(length(v),1); k = ( x <= v(1)); temp(1) = sum(k); for i = 2 : length(v) k = ((x > v(i-1)).*(x <= v(i))); temp(i) = sum(k); end c = temp;