Regarding loop structure in Matlab for an iterative procedure -
i'm trying code loop in matlab iteratively solves optimal vector s of zeros , ones. code
n = 150; s = ones(n,1); = 1:n if s(i) == 0 = + 1; else = i; end select = s; hi = (item_c' * (weights.*s)) * (1/(weights'*s)); s(i) = 0; ci = (item_c' * (weights.*s)) * (1/(weights'*s)); standarderror_afterex = sqrt(var(ci - cm)); standarderror_priorex = sqrt(var(hi - cm)); ratio = (standarderror_afterex - standarderror_priorex)/(abs(mean(weights.*s) - weights'*select)); ratios(i) = ratio; s(i) = 1; end [m,i] = min(ratios); s(i) = 0;
this code sets element 0 in s, has lowest ratio. need procedure start on again, using new s 1 zero, find ratios , exclude element in s has lowest ratio. need on , on until no ratios negative.
need loop, or miss something?
i hope question clear enough, tell me if need me explain more.
thank in advance, helping out newbie programmer.
edit
i think need add form of while loop well. can't see how structure this. flow want
with items included (s(i) = 1
i), calculate hi, ci , standard errors , list ratios, exclude item (s(i) = 0
) corresponds lowest negative ratio.
new s, including ones 1 zero, calculate hi, ci , standard errors , list ratios, exclude item i, corresponds lowest negative ratio.
new s, including ones 2 zeros, repeat process.
until there no negative element in ratios exclude.
hope got more clear now.
ok. want go through few things before list code. these how i try it. not best way, or fastest way (though i'd think it'd pretty quick). tried keep structure had in code, follow nicely (even though i'd meld calculations down single function or line).
some features i'm using in code:
bsxfun
: learn this! amazing how works , can speed code, , makes things easier.v = rand(n,1); = rand(n,4); % 2 lines below compute same value: w = bsxfun(@(x,y)x.*y,v,a); w_= repmat(v,1,4).*a;
bsxfun
dot multipliesv
vector each column ofa
. bothw
,w_
matrices same sizea
, first faster (usually).precalculating dropouts: made
select
matrix, before vector. allows me form variableincluded
using logical constructs.~(eye(n))
produces identity matrix , negates it. logically "and"ing select, $i$th column select, $i$th element dropped out.you explicitly calculating
weights'*s
denominator in each for-loop. using above matrix calculate this, cansum(w)
,w
weights.*s
in each column.take advantage of column-wise operations:
var()
,sqrt()
functions both coded work along columns of matrix, outputting action matrix in form of row vector.
ok. full thing. questions let me know:
% start selected: select = true(n); stop = false; % stopping flag: while (~stop) % each column leaves variable out... included = ~eye(n) & select; % calculates weights leave-one-out: w = bsxfun(@(x,y)x.*y,weights,included); % can comment out line below, if you'd like... w_= repmat(weights,1,n).*included; % same previous line. % calculates weights before dropping variables: v = bsxfun(@(x,y)x.*y,weights,select); % there's different syntax, depending on whether item_c % vector or matrix... if(isvector(item_c)) hi = (item_c' * v)./(sum(v)); ci = (item_c' * w)./(sum(w)); else % example: item_c matrix... % have use bsxfun() again hi = bsxfun(@rdivide, (item_c' * v),sum(v)); ci = bsxfun(@rdivide, (item_c' * w),sum(w)); end standarderror_afterex = sqrt(var(bsxfun(@minus,hi,cm))); standarderror_priorex = sqrt(var(bsxfun(@minus,ci,cm))); % or: % % standarderror_afterex = sqrt(var(hi - repmat(cm,1,size(hi,2)))); % standarderror_priorex = sqrt(var(ci - repmat(cm,1,size(ci,2)))); ratios = (standarderror_afterex - standarderror_priorex)./(abs(mean(w) - sum(v))); % identify negative ratios: negratios = ratios < 0; if ~any(negratios) % drop out of while-loop: stop = true; else % find negative ratio: neginds = find(negratios); [mn, mnind] = min(ratios(negratios)); % drop out negative one... select(neginds(mnind),:) = false; end end % end while(~stop) % output: s = select(:,1);
if reason doesn't work, please let me know.
Comments
Post a Comment