24小时热门版块排行榜    

Znn3bq.jpeg
查看: 1417  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考博] 博士自荐 +5 可可小胖 2026-04-08 5/250 2026-04-09 20:51 by leapmouse
[考研] 284求调剂 +8 让我上岸吧阿西 2026-04-09 10/500 2026-04-09 18:34 by 段伟艳
[考研] 349学科化学045106求调剂,化学类都可以 +8 保好懂懂 2026-04-08 8/400 2026-04-09 14:03 by xulei3024
[考研] 302分求调剂 +4 凡语祈愿 2026-04-08 5/250 2026-04-08 22:03 by 土木硕士招生
[考研] 275 求调剂 +8 Lei812514 2026-04-07 8/400 2026-04-08 12:46 by chemisry
[考研] 281求调剂 +10 椰子蘑菇 2026-04-06 10/500 2026-04-08 11:43 by zzucheup
[考研] 一志愿生物与医药,296分,求调剂 +14 66鹿 2026-04-03 16/800 2026-04-08 10:38 by tjzhao
[考研] 22408 调剂材料 +7 我叫ez 2026-04-06 8/400 2026-04-07 17:12 by 蓝云思雨
[考研] 生物学调剂 可调剂到生物与医药 +3 李政莹 2026-04-06 3/150 2026-04-06 19:02 by macy2011
[考研] 生物学学硕求调剂:351分一志愿南京师范大学生物学专业 +6 …~、王…~ 2026-04-06 7/350 2026-04-06 18:54 by macy2011
[考研] 一志愿北京交通大学材料工程总分358求调剂 +4 cs0106 2026-04-04 4/200 2026-04-05 18:46 by imissbao
[考研] 08专硕275调剂 +5 AaAa7420 2026-04-05 5/250 2026-04-05 18:01 by jkddd
[考研] 一志愿北交大材料工程总分358求调剂 +6 cs0106 2026-04-05 6/300 2026-04-05 16:34 by imissbao
[考研] 一志愿江南大学085501机械工程专硕326分,本科佳木斯大学 +5 顾若浮生 2026-04-03 9/450 2026-04-05 09:57 by 1753564080
[考研] 材料化工306分找合适调剂 +14 沧海轻舟e 2026-04-04 14/700 2026-04-05 09:53 by 朱云虎202
[考研] 283求调剂 +4 mcbbc 2026-04-03 5/250 2026-04-04 20:51 by imissbao
[考研] 一志愿东北大学085901土木专硕345求调剂 +3 zxt11111 2026-04-04 3/150 2026-04-04 14:21 by 土木硕士招生
[考研] 387求调剂 +4 爱吃片豆土 2026-04-03 5/250 2026-04-04 08:10 by 岸上的一条鱼
[考研] 工科341分调剂 +3 洛多罗 2026-04-03 3/150 2026-04-03 14:20 by 1753564080
[考研] 求调剂 +3 心想事成可 2026-04-03 3/150 2026-04-03 11:22 by wangjy2002
信息提示
请填处理意见