关于matlab函数的问题
我一现在用的matlab6.2,我想应用matlab6.5中的randsample函数。请问如何在matlab6.2中安装matlab6.5中的randsample函数呢?我是matlab的新手,谢谢各位大侠的帮助。[ 本帖最后由 xuebx 于 2006-9-2 10:29 编辑 ] 现在 还有 6.2吗?LZ真强
function y = randsample(n, k, replace, w)
%RANDSAMPLE Random sample, with or without replacement.
% Y = RANDSAMPLE(N,K) returns Y as a 1-by-K vector of values sampled
% uniformly at random, without replacement, from the integers 1:N.
%
% Y = RANDSAMPLE(POPULATION,K) returns K values sampled uniformly at
% random, without replacement, from the values in the vector POPULATION.
%
% Y = RANDSAMPLE(...,REPLACE) returns a sample taken with replacement if
% REPLACE is true, or without replacement if REPLACE is false (the default).
%
% Y = RANDSAMPLE(...,true,W) returns a weighted sample, using positive
% weights W, taken with replacement.W is often a vector of probabilities.
% This function does not support weighted sampling without replacement.
%
% Example:Generate a random sequence of the characters ACGT, with
% replacement, according to specified probabilities.
%
% R = randsample('ACGT',48,true,)
%
% See also RAND, RANDPERM.
% Copyright 1993-2004 The MathWorks, Inc.
% $Revision: 1.1.4.1 $$Date: 2003/11/01 04:28:51 $
if nargin < 2
error('stats:randsample:TooFewInputs','Requires two input arguments.');
elseif numel(n) == 1
population = [];
else
population = n;
n = numel(population);
if length(population)~=n
error('stats:randsample:BadPopulation','POPULATION must be a vector.');
end
end
if nargin < 3
replace = false;
end
if nargin < 4
w = [];
elseif ~isempty(w)
if length(w) ~= n
if isempty(population)
error('stats:randsample:InputSizeMismatch',...
'W must have length equal to N.');
else
error('stats:randsample:InputSizeMismatch',...
'W must have the same length as the population.');
end
else
p = w(:)' / sum(w);
end
end
switch replace
% Sample with replacement
case {true, 'true', 1}
if isempty(w)
y = ceil(n .* rand(k,1));
else
= histc(rand(k,1),);
end
% Sample without replacement
case {false, 'false', 0}
if k > n
if isempty(population)
error('stats:randsample:SampleTooLarge',...
'K must be less than or equal to N for sampling without replacement.');
else
error('stats:randsample:SampleTooLarge',...
'K must be less than or equal to the population size.');
end
end
if isempty(w)
% If the sample is a sizeable fraction of the population,
% just randomize the whole population (which involves a full
% sort of n random values), and take the first k.
if 4*k > n
rp = randperm(n);
y = rp(1:k);
% If the sample is a small fraction of the population, a full sort
% is wasteful.Repeatedly sample with replacement until there are
% k unique values.
else
x = zeros(1,n); % flags
sumx = 0;
while sumx < k
x(ceil(n * rand(1,k-sumx))) = 1; % sample w/replacement
sumx = sum(x); % count how many unique elements so far
end
y = find(x > 0);
y = y(randperm(k));
end
else
error('stats:randsample:NoWeighting',...
'Weighted sampling without replacement is not supported.');
end
otherwise
error('stats:randsample:BadReplaceValue',...
'REPLACE must be either true or false.');
end
if ~isempty(population)
y = population(y);
else
y = y(:);
end
这是原码 这样就可以啦 建议搂住使用6.5版本以上的matlab 你的函数代码是从哪里弄来的呀?但遗憾的是此函数还是在matlab6.2里面不能用,出现以下错误:??? Undefined function or variable 'false'.
Error in ==> F:\mymatlab\randsample.m
On line 39==> replace = false;看来还得用matlab7.0的了。我感觉7.0太大,6.5有时有字迹模糊的现象。谢谢你的帮助。 6.5中有randsample吗?我的好像没有啊 原帖由 ABBYABBIE 于 2006-9-2 13:08 发表
6.5中有randsample吗?我的好像没有啊
应该有的,用这个命令试一下
>> type randsample
页:
[1]