Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Efficiently updating a member of a cell array

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 23 Feb, 2010 21:56:22

Message: 1 of 13

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

Subject: Efficiently updating a member of a cell array

From: Wayne King

Date: 23 Feb, 2010 22:40:25

Message: 2 of 13

"Brendan " <btracey@stanford.edu> wrote in message <hm1iu6$i0k$1@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

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 24 Feb, 2010 00:50:22

Message: 3 of 13

"Wayne King" <wmkingty@gmail.com> wrote in message <hm1lgp$3qs$1@fred.mathworks.com>...
> "Brendan " <btracey@stanford.edu> wrote in message <hm1iu6$i0k$1@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

Subject: Efficiently updating a member of a cell array

From: Nathan

Date: 24 Feb, 2010 00:57:25

Message: 4 of 13

On Feb 23, 4:50 pm, "Brendan " <btra...@stanford.edu> wrote:
> "Wayne King" <wmkin...@gmail.com> wrote in message <hm1lgp$3q...@fred.mathworks.com>...
> > "Brendan " <btra...@stanford.edu> wrote in message <hm1iu6$i0...@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

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 24 Feb, 2010 01:12:04

Message: 5 of 13

Nathan <ngreco32@gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834e9e@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

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 24 Feb, 2010 01:15:07

Message: 6 of 13

Nathan <ngreco32@gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834e9e@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;

Subject: Efficiently updating a member of a cell array

From: Nathan

Date: 24 Feb, 2010 01:20:47

Message: 7 of 13

On Feb 23, 5:12 pm, "Brendan " <btra...@stanford.edu> wrote:
> Nathan <ngrec...@gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834...@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

Subject: Efficiently updating a member of a cell array

From: Nathan

Date: 24 Feb, 2010 01:31:26

Message: 8 of 13

On Feb 23, 5:15 pm, "Brendan " <btra...@stanford.edu> wrote:
> Nathan <ngrec...@gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834...@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

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 24 Feb, 2010 01:51:20

Message: 9 of 13

Nathan <ngreco32@gmail.com> wrote in message <f6694b6c-21c9-4d44-b6bd-a2fcce2edaa7@c34g2000pri.googlegroups.com>...
> On Feb 23, 5:12 pm, "Brendan " <btra...@stanford.edu> wrote:
> > Nathan <ngrec...@gmail.com> wrote in message <55880c2a-0994-40af-b08a-f7f7b7834...@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

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 24 Feb, 2010 02:07:02

Message: 10 of 13

Nathan <ngreco32@gmail.com> wrote in message <70c137ea-91ee-4cf6-aa1e-c52bf6f32ed2@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.

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 24 Feb, 2010 02:24:08

Message: 11 of 13

"Brendan " <btracey@stanford.edu> wrote in message <hm21k6$8s8$1@fred.mathworks.com>...
> Nathan <ngreco32@gmail.com> wrote in message <70c137ea-91ee-4cf6-aa1e-c52bf6f32ed2@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.

Nevermind on question 2, I understand it I think.

Subject: Efficiently updating a member of a cell array

From: Nathan

Date: 24 Feb, 2010 21:54:56

Message: 12 of 13

On Feb 23, 6:07 pm, "Brendan " <btra...@stanford.edu> wrote:
> Nathan <ngrec...@gmail.com> wrote in message <70c137ea-91ee-4cf6-aa1e-c52bf6f32...@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.

Ah, I accidentally left the jj part in there. Instead of looping
through cells, the cellfun takes care of each cell individually.

Was that code sufficient for you?

And for the second question, whether you understand it or not:
for ii=1:numIter
    newTermU=3*ii;
    tmp{ii} = cellfun(@(x)x(ii) + newTermU,EofGgivenXjU)
end
x is the parameter to the anonymous function that does x(ii) +
newTermU.
EofGgivenXjU is passed in as x, therefore for each cell in
EofGgivenXju, it will look for the ii'th item and add newTermU to it.

I didn't, at the time, have your D variable, so I used ii as a
temporary value for an example.

I hope that helps clear things up.

-Nathan

Subject: Efficiently updating a member of a cell array

From: Brendan

Date: 25 Feb, 2010 01:12:06

Message: 13 of 13

Nathan <ngreco32@gmail.com> wrote in message <7e92466f-add5-4d0f-abc1-40693e49ddbe@u19g2000prh.googlegroups.com>...
> On Feb 23, 6:07 pm, "Brendan " <btra...@stanford.edu> wrote:
> > Nathan <ngrec...@gmail.com> wrote in message <70c137ea-91ee-4cf6-aa1e-c52bf6f32...@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.
>
> Ah, I accidentally left the jj part in there. Instead of looping
> through cells, the cellfun takes care of each cell individually.
>
> Was that code sufficient for you?
>
> And for the second question, whether you understand it or not:
> for ii=1:numIter
> newTermU=3*ii;
> tmp{ii} = cellfun(@(x)x(ii) + newTermU,EofGgivenXjU)
> end
> x is the parameter to the anonymous function that does x(ii) +
> newTermU.
> EofGgivenXjU is passed in as x, therefore for each cell in
> EofGgivenXju, it will look for the ii'th item and add newTermU to it.
>
> I didn't, at the time, have your D variable, so I used ii as a
> temporary value for an example.
>
> I hope that helps clear things up.
>
> -Nathan

Thank you for your help. I'm still working things out, but at the very least my original question has been answered, and you have pointed me in the right path on step two :)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us