From: Jean-Luc Dellis on 16 Apr 2010 11:04 Hi Roy and all, I found your code does not work for scalars and correct some details: function v=randsampleWRW(x,k,w) % same than randsample but Without Replacement and with Weighting % Returns V, a weigthed sample of K elements taken among X without replacement % X a vector of numerics % K amount of element to sample from x % W a vector of positive weights w, whose length is length(x) % EXAMPLE: % for i=1:100 % v(i)=randsampleWRW([0,0.5,3,20],1,[0.5,0.4,0.05,0.05]); % end % plot(v,'o') % Was initialy made by ROY, i just correct some details and add help % see the link: % http://www.mathworks.com/matlabcentral/newsreader/view_thread/141124 % Jean-Luc Dellis, april 2010 if k>length(x), error('k must be smaller than length(x)'), end if ~isequal(length(x),length(w)),error('the weight W must have the length of X'),end v=zeros(1,k); for i=1:k v(i)=randsample(n,1,true,w); w(n==v(i))=0; w=w./sum(w); end end Best regards
From: Jean-Luc Dellis on 16 Apr 2010 11:32 Pfff, i made an error, puting x as input and letting n in the code... Also, i deleted the line which normalized w. Corrected below: function v=randsampleWRW(x,k,w) % same than randsample but Without Replacement and with Weighting % Returns V, a weigthed sample of K elements taken among X without replacement % X a vector of numerics % K amount of element to sample from x % W a vector of positive weights w, whose length is length(x) % EXAMPLE: % for i=1:100 % v(i)=randsampleWRW([0,0.5,3,20],1,[0.5,0.4,0.05,0.05]); % end % plot(v,'o') % Was initialy made by ROY, i just correct some details and add help % see the link: % http://www.mathworks.com/matlabcentral/newsreader/view_thread/141124 % Jean-Luc Dellis, april 2010 if k>length(x), error('k must be smaller than length(x)'), end if ~isequal(length(x),length(w)),error('the weight W must have the length of X'),end v=zeros(1,k); for i=1:k v(i)=randsample(x,1,true,w); w(x==v(i))=0; end end
|
Pages: 1 Prev: Help needed with neural network Next: ? remove dicom info |