⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rand_int.m

📁 我认为很不错的语音处理的matlab源代码
💻 M
📖 第 1 页 / 共 2 页
字号:
function [ndraw, count, count2, error]=rand_int(a, b, n, sorted, no_duplicates, make_plot)
% % Keywords, random integer, integer, random
% % 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Syntax;
% % 
% % [ndraw, count, count2, error]=rand_int(a, b, n, sorted, no_duplicates, make_plot);
% % 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Description
% % 
% % This program quickly and randomly selects n integers from a to b.
% % The uniform distribution is used so that all integers are more equally
% % probable.  No claims are made about the actual probability
% % distributions.
% %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Input Arguments      
% %
% % a and b are the lower and upper values of the range of integers.
% % Either a or b can be the higher and lower values of the range.
% % The defaults are a=1 and b=10.
% % 
% % n is the number of integers to select and can be a constant
% % or a two element vector.
% % The defaults are a=1 and b=10.
% % 
% % If n is a single number, then ndraw is a column vector.
% % The defaults are n=5.
% % 
% % If n is a two element vector then n specifies the size of the output
% % matrix.  Example: n=[mm nn]; size(ndraw)=[mm nn];
% %
% % The user specifies whether the output is sorted 1 or randomly ordered 0.
% % The default is not sorted.  If sorted the output is in ascending order.
% %
% % The user specifies to remove duplicate integers 0 or allow duplicate
% % integers 1.  The default is to remove duplicates.  If duplicates are
% % removed, then each integer can occur only once.
% % 
% % If make_plot equals 1, then a plot of the random integers is made. 
% % If make_plot does not equal 1, then no plots are made.
% % The default is to not make plots.
% % The x-axis is the row or column indices whichever has more indices.  
% % The y-axis is the value of the integers.
% %  
% % This option was created to visually check the uniformity of the output
% % distribution. 
% %  
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Output Arguments      
% % 
% % ndraw   is the matrix of random integers.
% % 
% %         If n is a single number then ndraw is a column vector.
% % 
% %         if n is a two element vector then ndraw is a matrix with
% %         size equal to n.  Example n=[mm nn]; size(ndraw)=[mm nn]
% % 
% % count   is the number of iterations of selecting integers.
% % 
% % count2  is the number of iterations of deleting extra random numbers
% % 
% % error   1 there is at least 1 error
% %         0 there are no errors
% % 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % 
% % Comments on Speed      
% %
% % This program quickly selects n integers even when
% % b-a is very large (See the Large Integer Range Examples).
% %
% % When duplicates are allowed the program just selects the required
% % number of integers.  It is very simple case.
% %
% % When duplicates are removed, three regimes are used to optimize speed.
% %
% % The code runs the fastest in regime 2 pure rejection code contributed
% % by John D'Errico (woodchips@rochester.rr.com).  The pure rejection 
% % code is forced to quit after 100 unsuccessful iterations, then A catch
% % is used to select the required number of integers.
% %
% % The code runs the slowest when min((b-a+1)-n, n)/(b-a) > 0.43
% % and utilizes a contributed alternative approach in this range
% % from Jos x@y.z.
% %
% % For all other cases, the program selects extra integers, then keeps the
% % unique integers and randomly deselects extra integers.
% % There is a selection while loop and a deselection while loop.
% %
% % See example for syntax and definitions
% % of the input and output arguments
% %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Example='';       % Large integer Range Example with very small n
%
% a=1;              % a is the lowest integer that can be selected.
%                   % default value is 1
%
% b=10^15;          % b is the highest integer that can be selected.
%                   % default value is 10
%
% n=5;              % n is the number of integers to select.
%                   % default value is 5
%                   %
%                   % n can be a constant or a two element vector
%                   %
%                   % if n is a constant then the output is a column
%                   % vector with n rows
%                   %
%                   % if n is a two element matrix [mm nn]
%                   % then the output is a matrix of size [mm nn]
%
% sorted=1;         % 1 output is sorted (ascending order)
%                   % otherwise not sorted
%                   % default value is 0 (not sorted)
%
% no_duplicates=1;  % 1 there are no duplicate integers in the output
%                   % otherwise allow duplicate integers
%                   % default value is 1 (duplicates removed)
% 
% make_plot=1;      % 1 make plots of the integers
%                   % otherwise do not make any plots
% 
% [ndraw, count]=rand_int(a, b, n, sorted, no_duplicates, make_plot);
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Example='';       % Very Large integer Range Example with small n
% 
% a=-10^15;
% b=10^15;
% n=1000;
% sorted=1;         %(ascending order)
% no_duplicates=1;
% [ndraw, count, count2]=rand_int(a,b,n,sorted, no_duplicates, make_plot);
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Example='';       % Large integer Range Example with large n
% 
% a=1;
% b=10^7;
% n=[2.5*10^6 2];  % note that n is a two element matrix and very large
% sorted=0;
% no_duplicates=0;
% [ndraw, count, count2]=rand_int(a,b,n, sorted, no_duplicates, make_plot);
% 
% Examples of the 3 regimes
% 
% Regime 1: Build entire matrix then remove extras
% [ndraw, count, count2, error]=rand_int(-10000, 10000, [ 1000 10 ], 0, 0, 1);
% 
% Regime 2: Pure rejection code (100 percent purge regime).
% [ndraw, count, count2, error]=rand_int(-10000, 10000, [ 10 1 ], 0, 0, 1);
% 
% Regime 3: Iteratively select integers then remove extras if necessary
% [ndraw, count, count2, error]=rand_int(-10000, 10000, [ 100 10 ], 0, 0, 1);
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Example=''; % Exceeding count limit.  The Pure Rejection Regime Failure
%
% % If the following code is run enough times the pure rejection regime
% % will fail and the catch will select the additional integers.
% 
% a=1;
% b=10000;
% n=250;  % note that n is a two element matrix and very large
% sorted=1;
% no_duplicates=1;
% [ndraw, count, count2]=rand_int(a,b,n, sorted, no_duplicates);
% 
% % The pure rejection regime is forced into failure if it fails to select 
% % enough integers after 100 iterations.  A catch is put into place 
% % to select  all the necessary integers.
% % 
% % 
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% % This program was written by Edward L. Zechmann
% %     date  23 January 2008
% %
% % modified  23 January 2008   updated comments
% %                             increased speed for a large range of
% %                             integers
% %
% % modified  24 January 2008   Modified the code to be faster
% %                             Added contributed alternative approach
% %                             from Jos x@y.z.
% %                             Created a second regime to quicken
% %                             integer selection when number of selected
% %                             integers is nearly one-half of the range
% %                             Added some large integer range examples
% %
% % modified  25 January 2008   Added sorted option for ordering the 
% %                             output array.  Now output can be sorted 
% %                             or random.
% %
% %                             Added an option for n to be a two element
% %                             matrix stipulating the output matrix size
% %                             so n=[mm nn];
% %                             mm is number of rows
% %                             nn is number of columns
% %
% % modified  26 January 2008   Updated Comments
% %
% %                             Fixed a bug so that output is within the
% %                             selected range.
% %
% %                             Added contributed code from
% %                             John D'Errico (woodchips@rochester.rr.com)
% %                             Created a third regime to quickly select
% %                             a very small number of integers.
% %
% %                             Added warnings and error output when input
% %                             does not make sense or is out of range
% %
% % modified  30 January 2008   Rearranged the three regimes so that
% %                             third regime can catch failures from the
% %                             100% purge regime when it exceeds
% %                             the max_count limit.
% %
% % modified  31 January 2008   Fixed bug.  The ouptput is reshaped
% %                             to the specified size or the best size
% %                             for ndraw
% %
% %                             The comments were revised and updated
% %
% % modified  20 February 2008  Added option to make a plot of the integers
% %                             Added additional examples.
% %                             Removed pure rejection regime warnings
% %                             since the catch is reliable.  
% %
% % ***********************************************************
% %
% % Feel free to modify this code.
% % 
% % See Also: MYRANDINT
% % 

% set the error flag to no error
% flag = 0 means there is typical output
% flag = 1 mean the output must be truncated because of bad input
flag=0;

if nargin < 1
    a=1;
end

if isempty(a)
    flag=1; % return an empty array and an error
    warning('Input error!  Argument a is empty.  Returning empty output array');
end

if nargin < 2
    b=10;
end

if isempty(b)
    flag=1; % return an empty array and an error
    warning('Input error!  Argument b is empty.  Returning empty output array');
end

if nargin < 3
    n=5;
end

if isempty(n)
    flag=1; % return an empty array and an error
    warning('Input error!  Argument n is empty.  Returning empty output array');
end

if nargin < 4 || isempty(sorted)
    sorted=0;
end

if nargin < 5 || isempty(no_duplicates)
    no_duplicates=1;
end

if ~isequal(sorted, 1)
    sorted=0;
end

if ~isequal(no_duplicates, 1)
    no_duplicates=0;
end

if nargin < 6
    make_plot=0;
end


ndraw=[];
count=0;
count2=0;
error=0;
mm=0;
nn=0;

if isequal(flag, 1)

else

    % set the error flag to no error
    % flag = 0 means there is typical output
    % flag = 1 mean the output must be truncated because of bad input
    flag=0;
    % set the other flags to null
    flag2=0;
    flag3=0;

    % make sure certain inputs are constant integers
    %
    % n can be a 2 element vector
    % round a up to nearest integer
    a=ceil(a(1));

    % round b down to nearest integer
    b=floor(b(1));

    % make sure n has only 2 dimensions
    n=round(n(:, :, 1));
    sorted=round(sorted(1));
    no_duplicates=round(no_duplicates(1));



    % If n is a constant the output is a column vector
    %
    % if n is a two element vector n=[mm nn];
    % Then the output matrix has size [mm nn];

    if length(n) > 1
        mm=n(1);
        nn=n(2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -