From: Brendan on 23 Feb 2010 20:15 Nathan <ngreco32(a)gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834e9e(a)k36g2000prb.googlegroups.com>... > > It does, thank you very much. If you don't mind a followup: > > > > Given the way my code is actually structured, I'm pretty sure I can't vectorize the outer loop, but is there a way to vectorize the inner loop, i.e. replace the for jj=1:numVars loop with some sort of colon structure? > > > > for ii=1:numIter > > newTermU=3*ii; > > for jj=1:numVars > > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > > end > > end > > Have you tried something like this: > > for ii=1:numIter > newTermU=3*ii; > jj=1:numVars; %jj is a vector to index the next line > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > end > > -Nathan Also doesn't work is aa=1:numVars bob=D(ii,aa) EofGgivenXjL{aa}(bob)=EofGgivenXjL{aa}(bob)+newTermL;
From: Nathan on 23 Feb 2010 20:20 On Feb 23, 5:12 pm, "Brendan " <btra...(a)stanford.edu> wrote: > Nathan <ngrec...(a)gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834...(a)k36g2000prb.googlegroups.com>... > > > Given the way my code is actually structured, I'm pretty sure I can't vectorize the outer loop, but is there a way to vectorize the inner loop, i.e. replace the for jj=1:numVars loop with some sort of colon structure? > > > > for ii=1:numIter > > > newTermU=3*ii; > > > for jj=1:numVars > > > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > > > end > > > end > > > Have you tried something like this: > > > for ii=1:numIter > > newTermU=3*ii; > > jj=1:numVars; %jj is a vector to index the next line > > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > > end > > > -Nathan > > I hadn't tried that, it's a nice thought, but it gives me a "Bad cell reference operation" error Eh. Was just a thought. Have you looked into cellfun? That will probably help. Unfortunately, I don't know what your code looks like and don't have test data present to help you further... Good luck. -Nathan
From: Nathan on 23 Feb 2010 20:31 On Feb 23, 5:15 pm, "Brendan " <btra...(a)stanford.edu> wrote: > Nathan <ngrec...(a)gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834...(a)k36g2000prb.googlegroups.com>... > > > It does, thank you very much. If you don't mind a followup: > > > > Given the way my code is actually structured, I'm pretty sure I can't vectorize the outer loop, but is there a way to vectorize the inner loop, i.e. replace the for jj=1:numVars loop with some sort of colon structure? > > > > for ii=1:numIter > > > newTermU=3*ii; > > > for jj=1:numVars > > > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > > > end > > > end > > > Have you tried something like this: > > > for ii=1:numIter > > newTermU=3*ii; > > jj=1:numVars; %jj is a vector to index the next line > > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > > end > > > -Nathan > > Also doesn't work is > aa=1:numVars > bob=D(ii,aa) > EofGgivenXjL{aa}(bob)=EofGgivenXjL{aa}(bob)+newTermL; Ah... I guess test data wasn't too tough to come up with. Here it is, and I hope this is what you're looking for: EofGgivenXjU{1}=[0,0,0,0]; EofGgivenXjU{2}=[1,1,1,1]; EofGgivenXjU{3}=[1:4]; numIter = length(EofGgivenXjU{1}); numVars = length(EofGgivenXjU); for ii=1:numIter newTermU=3*ii; jj=1:numVars; tmp{ii} = cellfun(@(x)x(ii) + newTermU,EofGgivenXjU) end Note that the tmp variable contains the new cell arrays. Disperse them as you like. -Nathan
From: Brendan on 23 Feb 2010 20:51 Nathan <ngreco32(a)gmail.com> wrote in message <f6694b6c-21c9-4d44-b6bd-a2fcce2edaa7(a)c34g2000pri.googlegroups.com>... > On Feb 23, 5:12 pm, "Brendan " <btra...(a)stanford.edu> wrote: > > Nathan <ngrec...(a)gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834...(a)k36g2000prb.googlegroups.com>... > > > > Given the way my code is actually structured, I'm pretty sure I can't vectorize the outer loop, but is there a way to vectorize the inner loop, i.e. replace the for jj=1:numVars loop with some sort of colon structure? > > > > > > for ii=1:numIter > > > > newTermU=3*ii; > > > > for jj=1:numVars > > > > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > > > > end > > > > end > > > > > Have you tried something like this: > > > > > for ii=1:numIter > > > newTermU=3*ii; > > > jj=1:numVars; %jj is a vector to index the next line > > > EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; > > > end > > > > > -Nathan > > > > I hadn't tried that, it's a nice thought, but it gives me a "Bad cell reference operation" error > > Eh. Was just a thought. > Have you looked into cellfun? That will probably help. Unfortunately, > I don't know what your code looks like and don't have test data > present to help you further... > > Good luck. > > -Nathan If you're willing to help, I have prepared a sample input for my subroutine. Note that in the general case, the q{iter,1} will not be the same number every time. You can see the two different for jj=1:numVars loops. Thank you very much for your help Input Routine: (note that it calls importance sampling at the end) clc clear all close all numVars=3; numDisc=[3 4 5]; maxIter=10; D=zeros(maxIter,numVars); GofX=zeros(maxIter,1); for iter=1:maxIter for ii=1:numVars q{iter,ii}=(1/numDisc(ii))*ones(1,numDisc(ii)); D(iter,ii)=randi(numDisc(ii),1,1); GofX(iter)=iter; end end qcurrent=cell(1,numVars); for var=1:numVars qcurrent{1,var}=q{maxIter,var}; end [EofG, EofGgivenXj]=importancesampling_test(q,D,GofX,qcurrent,numDisc) Subfunction function [EofG, EofGgivenXj]=importancesampling_test(q,D,GofX,qnew,numDisc) numVars=length(numDisc); EofGu=0; % Overall expected value of G upper sum EofGl=0; % " " lower sum EofGgivenXjU=cell(numVars,1); % Expected value of G given a certain value of X EofGgivenXjL=cell(numVars,1); EofGgivenXj=cell(numVars,1); for ii=1:numVars EofGgivenXj{ii}=zeros(1,numDisc(ii)); EofGgivenXjU{ii}=zeros(1,numDisc(ii)); EofGgivenXjL{ii}=zeros(1,numDisc(ii)); end for ii=1:length(GofX) qtheta=1; htheta=1; for jj=1:numVars qtheta=qtheta*qnew{1,jj}(D(ii,jj)); htheta=htheta*q{ii,jj}(D(ii,jj)); end newTermU=GofX(ii,1)*qtheta/htheta; newTermL=qtheta/htheta; EofGu=EofGu+newTermU; EofGl=EofGl+newTermL; for jj=1:numVars EofGgivenXjU{jj}(D(ii,jj))=EofGgivenXjU{jj}(D(ii,jj))+newTermU; end end EofG=EofGu/EofGl; for ii=1:numVars EofGgivenXj{ii}=EofGgivenXjU{ii}./(EofGl); end
From: Brendan on 23 Feb 2010 21:07 Nathan <ngreco32(a)gmail.com> wrote in message <70c137ea-91ee-4cf6-aa1e-c52bf6f32ed2(a)b36g2000pri.googlegroups.com>... > Ah... I guess test data wasn't too tough to come up with. Here it is, > and I hope this is what you're looking for: > EofGgivenXjU{1}=[0,0,0,0]; > EofGgivenXjU{2}=[1,1,1,1]; > EofGgivenXjU{3}=[1:4]; > numIter = length(EofGgivenXjU{1}); > numVars = length(EofGgivenXjU); > for ii=1:numIter > newTermU=3*ii; > jj=1:numVars; > tmp{ii} = cellfun(@(x)x(ii) + newTermU,EofGgivenXjU) > end > > Note that the tmp variable contains the new cell arrays. Disperse them > as you like. > > -Nathan Hey, sorry, I posted my thing before I saw this reply. Two points 1) In that code you have there it doesn't seem like you actually use jj anywhere 2) I was wondering if you could either explain or direct me to an explaination of the @(x)x(ii) part. I'm having trouble parsing what that means. Thanks again.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Time varying filter in Matlab (not simulink) Next: Simulation in simulink |