From: Brendan on 23 Feb 2010 16:56 Hello, In my code I have a cell array EofGgivenXjU which is a set of vectors. For example, EofGgivenXjU{1} is a 1x m vector, EofGgivenXjU{2} is a 1xn vector, etc. What I would like to do is update a specific position of that vector. For example, lets say that EofGgivenXjU{1}=[0,0,0,0], I would like to then make it =[0,1,0,0]. I have included what I am doing now, which includes two different ways I have come up with two different ways to do what I have proposed, but I'm wondering if there is a more efficient way. Specifically, numDisc(jj) is the length of the vector of EofGgivenXjU{jj}, and D(ii,jj) is a matrix which indicates which element I want to increment. for ii=1:numIter newTermU=3*ii; for jj=1:numVars blah=zeros(1,numDisc(jj)); blah(D(ii,jj))=blah(D(ii,jj))+newTermU; EofGgivenXjU{jj}=EofGgivenXjU{jj}+blah; blah2=EofGgivenXjL{jj}; blah2(D(ii,jj))=blah2(D(ii,jj))+newTermL; EofGgivenXjL{jj}=blah2; end end Thank you for your help
From: Wayne King on 23 Feb 2010 17:40 "Brendan " <btracey(a)stanford.edu> wrote in message <hm1iu6$i0k$1(a)fred.mathworks.com>... > Hello, > > In my code I have a cell array EofGgivenXjU which is a set of vectors. For example, EofGgivenXjU{1} is a 1x m vector, EofGgivenXjU{2} is a 1xn vector, etc. What I would like to do is update a specific position of that vector. For example, lets say that EofGgivenXjU{1}=[0,0,0,0], I would like to then make it =[0,1,0,0]. > > I have included what I am doing now, which includes two different ways I have come up with two different ways to do what I have proposed, but I'm wondering if there is a more efficient way. Specifically, numDisc(jj) is the length of the vector of EofGgivenXjU{jj}, and D(ii,jj) is a matrix which indicates which element I want to increment. > > for ii=1:numIter > newTermU=3*ii; > for jj=1:numVars > blah=zeros(1,numDisc(jj)); > blah(D(ii,jj))=blah(D(ii,jj))+newTermU; > EofGgivenXjU{jj}=EofGgivenXjU{jj}+blah; > > blah2=EofGgivenXjL{jj}; > blah2(D(ii,jj))=blah2(D(ii,jj))+newTermL; > EofGgivenXjL{jj}=blah2; > end > end > > Thank you for your help Hi Brendan, > What I would like to do is update a specific position of that vector. For example, lets >say that EofGgivenXjU{1}=[0,0,0,0], I would like to then make it =[0,1,0,0]. You can directly address a particular element of a cell array like this NewCell = cell(2,3); NewCell{1}=zeros(1,3); NewCell{1}(2)=1; % NewCell{1} is now 0 1 0 NewCell{1} For a matrix, use the regular matrix indexing to address elements: NewCell{2}=zeros(2,2); NewCell{2}(1,2)=1; NewCell{2} Hope that helps, Wayne
From: Brendan on 23 Feb 2010 19:50 "Wayne King" <wmkingty(a)gmail.com> wrote in message <hm1lgp$3qs$1(a)fred.mathworks.com>... > "Brendan " <btracey(a)stanford.edu> wrote in message <hm1iu6$i0k$1(a)fred.mathworks.com>... > > Hello, > > > > In my code I have a cell array EofGgivenXjU which is a set of vectors. For example, EofGgivenXjU{1} is a 1x m vector, EofGgivenXjU{2} is a 1xn vector, etc. What I would like to do is update a specific position of that vector. For example, lets say that EofGgivenXjU{1}=[0,0,0,0], I would like to then make it =[0,1,0,0]. > > > > I have included what I am doing now, which includes two different ways I have come up with two different ways to do what I have proposed, but I'm wondering if there is a more efficient way. Specifically, numDisc(jj) is the length of the vector of EofGgivenXjU{jj}, and D(ii,jj) is a matrix which indicates which element I want to increment. > > > > for ii=1:numIter > > newTermU=3*ii; > > for jj=1:numVars > > blah=zeros(1,numDisc(jj)); > > blah(D(ii,jj))=blah(D(ii,jj))+newTermU; > > EofGgivenXjU{jj}=EofGgivenXjU{jj}+blah; > > > > blah2=EofGgivenXjL{jj}; > > blah2(D(ii,jj))=blah2(D(ii,jj))+newTermL; > > EofGgivenXjL{jj}=blah2; > > end > > end > > > > Thank you for your help > > Hi Brendan, > > > What I would like to do is update a specific position of that vector. For example, lets >say that EofGgivenXjU{1}=[0,0,0,0], I would like to then make it =[0,1,0,0]. > You can directly address a particular element of a cell array like this > > NewCell = cell(2,3); NewCell{1}=zeros(1,3); > NewCell{1}(2)=1; > % NewCell{1} is now 0 1 0 > NewCell{1} > > For a matrix, use the regular matrix indexing to address elements: > > NewCell{2}=zeros(2,2); > NewCell{2}(1,2)=1; NewCell{2} > > Hope that helps, > Wayne 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
From: Nathan on 23 Feb 2010 19:57 On Feb 23, 4:50 pm, "Brendan " <btra...(a)stanford.edu> wrote: > "Wayne King" <wmkin...(a)gmail.com> wrote in message <hm1lgp$3q...(a)fred.mathworks.com>... > > "Brendan " <btra...(a)stanford.edu> wrote in message <hm1iu6$i0...(a)fred.mathworks.com>... > > > Hello, > > > > In my code I have a cell array EofGgivenXjU which is a set of vectors.. For example, EofGgivenXjU{1} is a 1x m vector, EofGgivenXjU{2} is a 1xn vector, etc. What I would like to do is update a specific position of that vector. For example, lets say that EofGgivenXjU{1}=[0,0,0,0], I would like to then make it =[0,1,0,0]. > > > > I have included what I am doing now, which includes two different ways I have come up with two different ways to do what I have proposed, but I'm wondering if there is a more efficient way. Specifically, numDisc(jj) is the length of the vector of EofGgivenXjU{jj}, and D(ii,jj) is a matrix which indicates which element I want to increment. > > > > for ii=1:numIter > > > newTermU=3*ii; > > > for jj=1:numVars > > > blah=zeros(1,numDisc(jj)); > > > blah(D(ii,jj))=blah(D(ii,jj))+newTermU; > > > EofGgivenXjU{jj}=EofGgivenXjU{jj}+blah; > > > > blah2=EofGgivenXjL{jj}; > > > blah2(D(ii,jj))=blah2(D(ii,jj))+newTermL; > > > EofGgivenXjL{jj}=blah2; > > > end > > > end > > > > Thank you for your help > > > Hi Brendan, > > > > What I would like to do is update a specific position of that vector. For example, lets >say that EofGgivenXjU{1}=[0,0,0,0], I would like to then make it =[0,1,0,0]. > > You can directly address a particular element of a cell array like this > > > NewCell = cell(2,3); NewCell{1}=zeros(1,3); > > NewCell{1}(2)=1; > > % NewCell{1} is now 0 1 0 > > NewCell{1} > > > For a matrix, use the regular matrix indexing to address elements: > > > NewCell{2}=zeros(2,2); > > NewCell{2}(1,2)=1; NewCell{2} > > > Hope that helps, > > Wayne > > 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
From: Brendan on 23 Feb 2010 20:12 Nathan <ngreco32(a)gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834e9e(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
|
Next
|
Last
Pages: 1 2 3 Prev: Time varying filter in Matlab (not simulink) Next: Simulation in simulink |