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

📄 wtrouselect.m

📁 物流分析工具包。Facility location: Continuous minisum facility location, alternate location-allocation (ALA)
💻 M
字号:
function Idx = wtrouselect(w,m,n)
%WTROUSELECT Weighted roulette selection.
%   Idx = wtrouselect(w,m,n)
%     w = p-element vector of weights
%   Idx = m x n matrix of random indices between 1 and p, where the
%         probability of selection for each index is proportional to its
%         weight; e.g., w = ones(1,p) => 1/p probability of selection
%
% Example:
% w = [0.5 0.25 0.25]         %  Half 1's, quarter 2's, and quarter 3's
% rand('state',141)           %  Only needed to duplicate this example
% Idx = wtrouselect(w,1,12)   %  Idx = 1  1  1  3  1  1  2  2  3  3  1  2

% Copyright (c) 1994-2006 by Michael G. Kay
% Matlog Version 9 13-Jan-2006 (http://www.ie.ncsu.edu/kay/matlog)

% Input Error Checking ****************************************************
error(nargchk(1,3,nargin));

if nargin < 2 | isempty(m), m = 1; end
if nargin < 3 | isempty(n), n = 1; end
% End (Input Error Checking) **********************************************

if sum(w(:)) == 0, Idx = NaN; return, end

d = [0 cumsum(w(:)')/sum(w(:))];
r = rand(m,n);

for i = 1:m
   for j = 1:n
      Idx(i,j)  = find(r(i,j) >= d(1:end-1) & r(i,j) < d(2:end));
   end
end

⌨️ 快捷键说明

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