24小时热门版块排行榜    

查看: 1268  |  回复: 8

xmcrobbie

银虫 (小有名气)

[求助] 有段程序,就5,6行,我看了N天了,请教高手

function Y = sampleWithR(weights,K)
%Y = sampleWithR(weights,K)
%Generates K samples from the discrete distribution specified by weights
%O(K)

%Need to handle border effects
cdf = cumsum(weights);
Y = histc(rand(K,1)*cdf(end),[0; cdf]);
Y = [Y(1:end-2); Y(end-1)+Y(end)];


我不知道这个函数起什么作用啊。。。。
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

haier20022

新虫 (正式写手)

【答案】应助回帖

感谢参与,应助指数 +1
是matlab程序吧?
2楼2013-10-20 13:33:29
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

feixiaolin

荣誉版主 (文坛精英)

优秀版主

function Y = sampleWithR(weights,K)
% 依权值重采样;
cdf = cumsum(weights);
% 权值累加
Y = histc(rand(K,1)*cdf(end),[0; cdf]);
% 统计直方图
Y = [Y(1:end-2); Y(end-1)+Y(end)];
% 直方图最末一bin修正
3楼2013-10-20 14:40:26
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xmcrobbie

银虫 (小有名气)

引用回帖:
3楼: Originally posted by feixiaolin at 2013-10-20 14:40:26
function Y = sampleWithR(weights,K)
% 依权值重采样;
cdf = cumsum(weights);
% 权值累加
Y = histc(rand(K,1)*cdf(end),);
% 统计直方图
Y = ;
% 直方图最末一bin修正...

Y = histc(rand(K,1)*cdf(end),[0; cdf]);  这行不理解啊。rand(K,1)是随机产生K个0到1的数吧。[0; cdf] 应该是在在cdf2前加个0吧。

应该是看看产生的这K个数,落在统计直方图中各个bin的个数吧。。。

这么理解对吗?
4楼2013-10-20 16:30:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

coolslj

金虫 (正式写手)

【答案】应助回帖

★ ★ ★ ★ ★
感谢参与,应助指数 +1
xmcrobbie: 金币+2 2013-11-04 15:32:52
xmcrobbie: 金币+3 2013-11-04 15:33:37
Generates K samples from the discrete distribution specified by weights
根据离散分布进行采样,该分布用样本权重表示

rand(K,1)*cdf(end)的目的是:将产生的0-1之间的随机数,放缩到上述离散分布(因为权重weights的总和可能不等于1)。不妨假设rand(K,1)返回1,就容易理解它的含义了。

[0; cdf] 的含义与cumsum的具体实现有关。
5楼2013-10-21 08:20:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xmcrobbie

银虫 (小有名气)

引用回帖:
5楼: Originally posted by coolslj at 2013-10-21 08:20:40
Generates K samples from the discrete distribution specified by weights
根据离散分布进行采样,该分布用样本权重表示

rand(K,1)*cdf(end)的目的是:将产生的0-1之间的随机数,放缩到上述离散分布(因为权重 ...

实际上Y矩阵是随机数在weighs直方图的分布,Y的值weighs的值并没有直接关系,不知道这样子采样有什么意义。
6楼2013-10-21 15:10:19
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

coolslj

金虫 (正式写手)

引用回帖:
6楼: Originally posted by xmcrobbie at 2013-10-21 15:10:19
实际上Y矩阵是随机数在weighs直方图的分布,Y的值weighs的值并没有直接关系,不知道这样子采样有什么意义。...

楼主没有提供该程序的背景和上下文,所以专家们有点盲人摸象。

该程序根据一个分布的直方图,产生了一个新的分布的直方图。
从这个角度看,该程序无实际意义。
我推测它主要是用于教学。
7楼2013-10-22 12:50:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xmcrobbie

银虫 (小有名气)

引用回帖:
7楼: Originally posted by coolslj at 2013-10-22 12:50:06
楼主没有提供该程序的背景和上下文,所以专家们有点盲人摸象。

该程序根据一个分布的直方图,产生了一个新的分布的直方图。
从这个角度看,该程序无实际意义。
我推测它主要是用于教学。...

function res = searchImCostTRandEndc (im,len,wid,MAX_NUM,thresh,currAngles)
%[res,respOut] = getSegments (im,frameNum,len,wid,thresh,currAngles)
%Searches binary im for patches of size len by wid of white
%Randomized version, thresh is the number of examples to return

SUPPRESS = 0;
SCALE = 1/3; %When we weight the background flanks by .5
%SCALE = 1/2;

%We want our final log potentials to be between 0 & 10
%FINAL_SCALE = 1/3;

[origY, origX, dummy] = size(im);


if ~exist('currAngles'),
  currAngles = 15:15:360;
  %currAngles = 15:15:180;
end
if ~exist('roi'),
  roi = [1 1;origY origX];
end

%Thresh is the number of pixels we're allowing to be misclassified
if ~exist('thresh') | isempty(thresh),
    %thresh = 1*len*wid;
    thresh = 0;
end

%Convert to a probability
%thresh = exp(-thresh/SCALE);

%Add the size of our patch to buffer our ROI
roi = round(roi + max([len wid])*[-1 -1;1 1]);

%Make sure ROI does not exceed our image dimensions
roi(1, = max([1 1],roi(1,);
roi(2, = min([origY origX],roi(2,);

%dtheta = 15;
ddir = 1; %Maybe go back to 2?
ddir = min(length(currAngles)-1,ddir);
dpos = 1; %Maybe go back to 2?

im = im(roi(1,1):roi(2,1),roi(1,2):roi(2,2),;
[imy,imx,dummy] = size(im);

%Shift our currentAngle
currAngles = -90 - currAngles;

[len,wid] = deal(round(len),round(wid));

%im = uint8(im);
%wwid = ceil(wid/8);
%wwid = 2;

%imPatch = [zeros(len,wid) ones(len,wid) zeros(len,wid)];
%Use felzenswchab style likelihood
%imPatch = [zeros(wid,wid*3); -ones(len,wid) ones(len,wid) -ones(len,wid); -ones(wid,wid*3)];

yy = [-(len-1)/2len-1)/2].^2/(.4*len.^2);
xx = [-(wid-1)/2wid-1)/2].^2/(.4*wid.^2);
[yy,xx] = ndgrid(yy,xx);
kk = exp(-(yy + xx));
kk = kk.*(len*wid/(sum(kk()));
imPatch = [zeros(wid,wid*3); -ones(len,wid) kk -ones(len,wid); -ones(wid,wid*3)];

%yind = 1:len; yind = yind - mean(yind);
%xind = 1:wid; xind = xind - mean(xind);
%[Y,X] = ndgrid(yind/(5*len),xind/(5*wid));
%imPatch = exp(-Y.^2 + -X.^2);

resp = zeros([length(currAngles) imy imx]);
for dir = 1:length(currAngles),
  currAngle = -90 - currAngles(dir);
  kernal = imrotate(imPatch,currAngle);

  %kernal = kernal./sum(kernal();  
  %kernal = kernal./sum(kernal(kernal > 0));

  [mm,nn,dummy] = size(kernal);
  
  %currResp = filter2(kernal == 1,im == 0,'valid') + .5*filter2(kernal == 0,im == 1,'valid');
  %tmp = filter2(kernal == 1,im == 0,'valid');
  tmp = filter2(kernal.*(kernal > 0),im == 0,'valid');
  currResp = tmp + .25*filter2(kernal < 0,im == 1,'valid');
  indy = ceil(mm/2):imy-floor(mm/2);
  indx = ceil(nn/2):imx-floor(nn/2);
  if ~isempty(currResp),
    %resp(dir,indy,indx) = exp(-currResp/SCALE);  
    %currResp = exp(-currResp/SCALE);
    currResp = exp(-currResp/(len*wid*SCALE));
    %currResp(tmp == sum(kernal( == 1)) = 0;
    currResp(tmp == sum(kernal(kernal > 0))) = 0;
    resp(dir,indy,indx) = currResp;
  end
end

%keyboard;
resp(resp < thresh) = 0;
%ord = find(sampleWithR(exp(-resp(/(len*wid)),thresh));

%%%%%%%%%%%%在这里调用了sampleWithR%%%%%%%
ord = find(sampleWithR(resp(,MAX_NUM));
resp = resp(ord);
[resp,I] = sort(resp);
resp = flipud(resp);
ord = ord(flipud(I));
%ord = ord(I);

res.resp = resp;
%res.resp = resp.^(1/FINAL_SCALE);

[ang i j] = ndgrid(pi/180*currAngles,roi(1,1):roi(2,1), roi(1,2):roi(2,2));

res.resp = resp;
ang = ang(ord); res.u = cos(ang); res.v = sin(ang);
res.x = j(ord);
res.y = i(ord);
res.len = len/2*ones(size(resp));
res.w = wid/2*ones(size(resp));
8楼2013-10-22 14:30:50
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xmcrobbie

银虫 (小有名气)

代码太长,专家们有空帮我看看么,我看了一个星期了,还是没搞懂!
9楼2013-10-22 14:34:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 xmcrobbie 的主题更新
信息提示
请填处理意见