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:
9 dimensional grid

Subject: 9 dimensional grid

From: Saad

Date: 13 Aug, 2011 13:02:29

Message: 1 of 15

Dear all,

I would like to create a 9 D grid. I tried with ndgrid but it doesnt allow me because it exceeds the numbers of dimensions allowed.
How can I create a 9 D grid please.?

Actually my aim is to evaluate a function at each grid point i of the first grid and then loop from 1 grid to grid 9. So the first loop would loop btw the grid points in grid 1 and the second loop would loop from grid 1 to grid 9. How can i set up that please? Thank you very much

best Regards

S

Subject: 9 dimensional grid

From: dpb

Date: 13 Aug, 2011 14:08:45

Message: 2 of 15

On 8/13/2011 8:02 AM, Saad wrote:
> Dear all,
> I would like to create a 9 D grid. I tried with ndgrid but it doesnt
> allow me because it exceeds the numbers of dimensions allowed.

Really?

I just tried a (very small) 9D grid w/ ndgrid() here and it succeed.

 >> ndgrid(1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3);
 >> whos ans
   Name Size Bytes Class

   ans 9-D 157464 double array

Grand total is 19683 elements using 157464 bytes

 >>
Are you sure it isn't memory not the number of dimensions; an n^9
expansion is going to grow quite large pretty quickly. What were the
inputs to ndgrid and the actual error, specifically (cut 'n paste, don't
paraphrase)

> How can I create a 9 D grid please.?
...

See above...

--

Subject: 9 dimensional grid

From: Saad

Date: 13 Aug, 2011 14:30:29

Message: 3 of 15

dpb <none@non.net> wrote in message <j260hc$paf$1@speranza.aioe.org>...
> On 8/13/2011 8:02 AM, Saad wrote:
> > Dear all,
> > I would like to create a 9 D grid. I tried with ndgrid but it doesnt
> > allow me because it exceeds the numbers of dimensions allowed.
>
> Really?
>
> I just tried a (very small) 9D grid w/ ndgrid() here and it succeed.
>
> >> ndgrid(1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3,1:3);
> >> whos ans
> Name Size Bytes Class
>
> ans 9-D 157464 double array
>
> Grand total is 19683 elements using 157464 bytes
>
> >>
> Are you sure it isn't memory not the number of dimensions; an n^9
> expansion is going to grow quite large pretty quickly. What were the
> inputs to ndgrid and the actual error, specifically (cut 'n paste, don't
> paraphrase)
>
> > How can I create a 9 D grid please.?
> ...
>
> See above...
>
> --
Hi
Thank you very much for your answer. When I try it with just 5-D it doesnt work. For example if I write this:

[A, B, C, D, E]=ndgrid( -10:0.1:10,-10:0.1:10, -10:0.1:10, -10:0.1:10, -10:0.1:10)

Here is the error:

??? Maximum variable size allowed by the program is exceeded.

Error in ==> ndgrid at 47
  x = reshape(x(:,ones(1,prod(s))),[length(x) s]); % Expand x



My problem is a bit tricky. in fact in each grid point i of grid 1, I have to initiliaze a set of parameters and these same parameters have to change at each grid point i of grid 1. How do you think I could set up that? Thanks a lot

Best

S

Subject: 9 dimensional grid

From: Matt J

Date: 13 Aug, 2011 16:45:31

Message: 4 of 15

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j261q5$9sk$1@newscl01ah.mathworks.com>...
>
> Thank you very much for your answer. When I try it with just 5-D it doesnt work. For example if I write this:
>
> [A, B, C, D, E]=ndgrid( -10:0.1:10,-10:0.1:10, -10:0.1:10, -10:0.1:10, -10:0.1:10)

You're asking for way too much data to be created.
Since length( -10:0.1:10 ) = 201
then numel(A) would have to be
201^5=3.2808e+011
and similarly for B,C,D,E...

Start from scratch. Explain what you're trying to do and the community may have an idea for a revised approach.

Subject: 9 dimensional grid

From: Saad

Date: 13 Aug, 2011 17:25:10

Message: 5 of 15

"Matt J" wrote in message <j269nb$22k$1@newscl01ah.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j261q5$9sk$1@newscl01ah.mathworks.com>...
> >
> > Thank you very much for your answer. When I try it with just 5-D it doesnt work. For example if I write this:
> >
> > [A, B, C, D, E]=ndgrid( -10:0.1:10,-10:0.1:10, -10:0.1:10, -10:0.1:10, -10:0.1:10)
>
> You're asking for way too much data to be created.
> Since length( -10:0.1:10 ) = 201
> then numel(A) would have to be
> 201^5=3.2808e+011
> and similarly for B,C,D,E...
>
> Start from scratch. Explain what you're trying to do and the community may have an idea for a revised approach.

Hi Matt
Thank you for your reply. I would like to minimize a function say "H". In each grid point, I would like to initialize 9 parameters, these parameters are inside the function. each grid point will give me one value.
First I would like to evaluate the function at one grid point then move to next grid point , change the parameters and evaluate the function again, then keep doing that accross "grid 1" and then at the end I will pick the function with the minimum value at grid 1. The parameters that give me the minimum value of function will be the chosen ones.

Then when I finish with grid 1 I would like to move to grid 2 and do the same analysis. if the minimum value of grid 1 is not much different from the minimum value of grid 2 then I will stop the iteration. So I dont necessarily need to create 9D grid from the beginning because i might stop half way.
Any advice or suggestion on how to set up this structure is much appreciated. Thank you very much again

Best

S

Subject: 9 dimensional grid

From: dpb

Date: 13 Aug, 2011 18:26:34

Message: 6 of 15

On 8/13/2011 12:25 PM, Saad wrote:
> "Matt J" wrote in message <j269nb$22k$1@newscl01ah.mathworks.com>...
>> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message
>> <j261q5$9sk$1@newscl01ah.mathworks.com>...
>> >
>> > Thank you very much for your answer. When I try it with just 5-D it
>> doesnt work. For example if I write this:
>> > > [A, B, C, D, E]=ndgrid( -10:0.1:10,-10:0.1:10, -10:0.1:10,
>> -10:0.1:10, -10:0.1:10)
>>
>> You're asking for way too much data to be created.
>> Since length( -10:0.1:10 ) = 201
>> then numel(A) would have to be 201^5=3.2808e+011
>> and similarly for B,C,D,E...
>>
>> Start from scratch. Explain what you're trying to do and the community
>> may have an idea for a revised approach.
>
> Hi Matt
> Thank you for your reply. I would like to minimize a function say "H".
...

Then use one of the minimization routines supplied in Matlab; which one
precisely would depend on the form of the function and whether the
parameters are constrained or not.

--

Subject: 9 dimensional grid

From: Matt J

Date: 13 Aug, 2011 20:25:11

Message: 7 of 15

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j26c1m$8dn$1@newscl01ah.mathworks.com>...
>
> Thank you for your reply. I would like to minimize a function say "H". In each grid point, I would like to initialize 9 parameters, these parameters are inside the function. each grid point will give me one value.
> First I would like to evaluate the function at one grid point then move to next grid point , change the parameters and evaluate the function again, then keep doing that accross "grid 1" and then at the end I will pick the function with the minimum value at grid 1. The parameters that give me the minimum value of function will be the chosen ones.
==================

Saad - this was a little hard to understand, but here's what I was able to get from it. You have a function of 9 parameters

  H( x(1) ,...., x(9) )

You're trying to minimize H by testing every combination of values for the x(i) on some discretized grid.

If that's what you're trying to do, it's a hopeless approach. The number of values to be explored is just way to big. As dbp was saying, you should be using a minimization algorithm, e.g. from the Optimization Toolbox. Avoiding exhaustive searches of the parameter space is what optimization algorithms are meant for.




> Then when I finish with grid 1 I would like to move to grid 2 and do the same analysis. if the minimum value of grid 1 is not much different from the minimum value of grid 2 then I will stop the iteration. So I dont necessarily need to create 9D grid from the beginning because i might stop half way.
=========================


You're basically describing the coordinate descent algorithm, where you minimize with respect to 1 parameter at a time while leaving the others fixed. Normally, though, you'd have to cycle through all 9 parameters many times before the value of the function stops changing. You'd be very lucky if this weren't the case.

If you want to use this approach you don't need NDGRID. Just compute

grid1D= -10:.1:10;

 [Hval,idx] = min( H(grid1D, x(2),x(3),...,x(9)))
  x(1)=grid1D(idx);

then

 [Hval,idx] = min( H(x(1), grid1D, x(3),...,x(9)))
 x(2)=grid1D(idx);



then

 [Hval,idx] = min( H(x(1),x(2), grid1D, x(4),...,x(9)))
 x(3)=grid1D(idx);



and so on in a loop until Hval stops changing. It's hard to know how fast it will converge, however. That will depend very much on the form of H.

Subject: 9 dimensional grid

From: Saad

Date: 15 Aug, 2011 12:31:25

Message: 8 of 15

"Matt J" wrote in message <j26mj7$7nm$1@newscl01ah.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j26c1m$8dn$1@newscl01ah.mathworks.com>...
> >
> > Thank you for your reply. I would like to minimize a function say "H". In each grid point, I would like to initialize 9 parameters, these parameters are inside the function. each grid point will give me one value.
> > First I would like to evaluate the function at one grid point then move to next grid point , change the parameters and evaluate the function again, then keep doing that accross "grid 1" and then at the end I will pick the function with the minimum value at grid 1. The parameters that give me the minimum value of function will be the chosen ones.
> ==================
>
> Saad - this was a little hard to understand, but here's what I was able to get from it. You have a function of 9 parameters
>
> H( x(1) ,...., x(9) )
>
> You're trying to minimize H by testing every combination of values for the x(i) on some discretized grid.
>
> If that's what you're trying to do, it's a hopeless approach. The number of values to be explored is just way to big. As dbp was saying, you should be using a minimization algorithm, e.g. from the Optimization Toolbox. Avoiding exhaustive searches of the parameter space is what optimization algorithms are meant for.
>
>
>
>
> > Then when I finish with grid 1 I would like to move to grid 2 and do the same analysis. if the minimum value of grid 1 is not much different from the minimum value of grid 2 then I will stop the iteration. So I dont necessarily need to create 9D grid from the beginning because i might stop half way.
> =========================
>
>
> You're basically describing the coordinate descent algorithm, where you minimize with respect to 1 parameter at a time while leaving the others fixed. Normally, though, you'd have to cycle through all 9 parameters many times before the value of the function stops changing. You'd be very lucky if this weren't the case.
>
> If you want to use this approach you don't need NDGRID. Just compute
>
> grid1D= -10:.1:10;
>
> [Hval,idx] = min( H(grid1D, x(2),x(3),...,x(9)))
> x(1)=grid1D(idx);
>
> then
>
> [Hval,idx] = min( H(x(1), grid1D, x(3),...,x(9)))
> x(2)=grid1D(idx);
>
>
>
> then
>
> [Hval,idx] = min( H(x(1),x(2), grid1D, x(4),...,x(9)))
> x(3)=grid1D(idx);
>
>
>
> and so on in a loop until Hval stops changing. It's hard to know how fast it will converge, however. That will depend very much on the form of H.


Hi Matt

Thanks a lot for your reply. What you wrote "You're basically describing the coordinate descent algorithm, where you minimize with respect to 1 parameter at a time while leaving the others fixed" is exactly what I would like to do. The only issue is that one of the parameters can not be negative but the others could be negative. Is it still possible to run the code above with this constraint? How do you think can I adapt the code? Thanks a lot for your advice your help is much appreciated

Best Regards
S

Subject: 9 dimensional grid

From: Matt J

Date: 15 Aug, 2011 13:12:11

Message: 9 of 15

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2b3it$ag9$1@newscl01ah.mathworks.com>...
>
>
> Thanks a lot for your reply. What you wrote "You're basically describing the coordinate descent algorithm, where you minimize with respect to 1 parameter at a time while leaving the others fixed" is exactly what I would like to do. The only issue is that one of the parameters can not be negative but the others could be negative. Is it still possible to run the code above with this constraint? How do you think can I adapt the code? Thanks a lot for your advice your help is much appreciated
=====================

There's no reason why you can't use a different grid1D for every parameter.

Subject: 9 dimensional grid

From: Saad

Date: 16 Aug, 2011 14:10:27

Message: 10 of 15

"Matt J" wrote in message <j2b5vb$i8b$1@newscl01ah.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2b3it$ag9$1@newscl01ah.mathworks.com>...
> >
> >
> > Thanks a lot for your reply. What you wrote "You're basically describing the coordinate descent algorithm, where you minimize with respect to 1 parameter at a time while leaving the others fixed" is exactly what I would like to do. The only issue is that one of the parameters can not be negative but the others could be negative. Is it still possible to run the code above with this constraint? How do you think can I adapt the code? Thanks a lot for your advice your help is much appreciated
> =====================
>
> There's no reason why you can't use a different grid1D for every parameter.

Hi Matt

Thank you very much. I am sorry to bother you again. This hopefully will be the last question. how you would loop the code that you suggested below. For example how is it possible to change the positioning of grid1D inside the function? For example at iteration 1 H(grid1D, x(2),x(3),...,x(9))), then iteration 2 H(x(1), grid1D, x(3),...,x(9)) and so on... I tried many tricks but it didnt work. Thanks a lot for your advice on this. Best. S


grid1D= -10:.1:10;

 [Hval,idx] = min( H(grid1D, x(2),x(3),...,x(9)))
  x(1)=grid1D(idx);

then
 [Hval,idx] = min( H(x(1), grid1D, x(3),...,x(9)))
 x(2)=grid1D(idx);

then

 [Hval,idx] = min( H(x(1),x(2), grid1D, x(4),...,x(9)))
 x(3)=grid1D(idx);

Subject: 9 dimensional grid

From: Matt J

Date: 16 Aug, 2011 14:27:29

Message: 11 of 15

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2dtoj$n4c$1@newscl01ah.mathworks.com>...
>
> Thank you very much. I am sorry to bother you again. This hopefully will be the last question. how you would loop the code that you suggested below. For example how is it possible to change the positioning of grid1D inside the function? For example at iteration 1 H(grid1D, x(2),x(3),...,x(9))), then iteration 2 H(x(1), grid1D, x(3),...,x(9)) and so on... I tried many tricks but it didnt work. Thanks a lot for your advice on this. Best. S
=========================

You'd do something like the following:

arguments=num2cell(x);

for k=1:9 %loop over the 9 parameters

  arguments{k}=grid1D; %Or, make grid1D depend on k also

  [Hval,idx] = min( H(arguments{:}) ); %Do you understand this. See help docs about comma-separated lists if not!!!

  arguments{k}=grid1D(idx);

end

x=cell2mat(arguments);

Subject: 9 dimensional grid

From: Saad

Date: 17 Aug, 2011 17:51:10

Message: 12 of 15

"Matt J" wrote in message <j2duoh$qol$1@newscl01ah.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2dtoj$n4c$1@newscl01ah.mathworks.com>...
> >
> > Thank you very much. I am sorry to bother you again. This hopefully will be the last question. how you would loop the code that you suggested below. For example how is it possible to change the positioning of grid1D inside the function? For example at iteration 1 H(grid1D, x(2),x(3),...,x(9))), then iteration 2 H(x(1), grid1D, x(3),...,x(9)) and so on... I tried many tricks but it didnt work. Thanks a lot for your advice on this. Best. S
> =========================
>
> You'd do something like the following:
>
> arguments=num2cell(x);
>
> for k=1:9 %loop over the 9 parameters
>
> arguments{k}=grid1D; %Or, make grid1D depend on k also
>
> [Hval,idx] = min( H(arguments{:}) ); %Do you understand this. See help docs about comma-separated lists if not!!!
>
> arguments{k}=grid1D(idx);
>
> end
>
> x=cell2mat(arguments);

Hi Matt

Thanks for the code it is really helpful. Correct me if I am wrong but the code that you suggested only allow to loop the positioning of the grid1D in the function "H" but doesnt allow the evaluation of the function at each grid point in grid1D? My aim is to create a second loop (inside the loop of "k=1:9") where I can access the element of gridD1 and evaluate the function at each grid point.
The only problem is that I dont know how to access the element of the grid1D in the second for loop. For example,

grid1D= -10:1:10;

 arguments=num2cell(x);
 
 for k=1:9
 
   arguments{k}=grid1D;

v=length(grid1D)

for i=1:v
 

%% in this loop I would like to access the elements of the grid1D and evaluate the function at each iteration. Usually to access the element of cell we do the following arguments{1}(1). But in this case it doesnt apply.

 end

% after the loop ends I will take the minimum value of the function.

   [Hval,idx] = min( H(arguments{:}) );
 
   arguments{k}=grid1D(idx);

 end
 
 x=cell2mat(arguments);

Any sample code would be very helpful. Thanks a lot for your guidance on this one.

Best

S

Subject: 9 dimensional grid

From: Matt J

Date: 17 Aug, 2011 18:15:10

Message: 13 of 15

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2gv2e$s0n$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <j2duoh$qol$1@newscl01ah.mathworks.com>...
> > "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2dtoj$n4c$1@newscl01ah.mathworks.com>...
> > >
> > > Thank you very much. I am sorry to bother you again. This hopefully will be the last question. how you would loop the code that you suggested below. For example how is it possible to change the positioning of grid1D inside the function? For example at iteration 1 H(grid1D, x(2),x(3),...,x(9))), then iteration 2 H(x(1), grid1D, x(3),...,x(9)) and so on... I tried many tricks but it didnt work. Thanks a lot for your advice on this. Best. S
> > =========================
> >
> > You'd do something like the following:
> >
> > arguments=num2cell(x);
> >
> > for k=1:9 %loop over the 9 parameters
> >
> > arguments{k}=grid1D; %Or, make grid1D depend on k also
> >
> > [Hval,idx] = min( H(arguments{:}) ); %Do you understand this. See help docs about comma-separated lists if not!!!
> >
> > arguments{k}=grid1D(idx);
> >
> > end
> >
> > x=cell2mat(arguments);
>
> Hi Matt
>
> Thanks for the code it is really helpful. Correct me if I am wrong but the code that you suggested only allow to loop the positioning of the grid1D in the function "H" but doesnt allow the evaluation of the function at each grid point in grid1D? My aim is to create a second loop (inside the loop of "k=1:9") where I can access the element of gridD1 and evaluate the function at each grid point.
> The only problem is that I dont know how to access the element of the grid1D in the second for loop. For example,
>
> grid1D= -10:1:10;
>
> arguments=num2cell(x);
>
> for k=1:9
>
> arguments{k}=grid1D;
>
> v=length(grid1D)
>
> for i=1:v
>
>
> %% in this loop I would like to access the elements of the grid1D and evaluate the function at each iteration. Usually to access the element of cell we do the following arguments{1}(1). But in this case it doesnt apply.
>

Why not access them as grid1D(i)?

In any case, you should only do this if you cannot vectorize the evaluation of H. I was under the impression that when you evaluate
H(w,x,y,z,...) the argument list w,x,y,z, etc... can consist of scalars and 1 vector

Subject: 9 dimensional grid

From: Saad

Date: 18 Aug, 2011 09:30:12

Message: 14 of 15

"Matt J" wrote in message <j2h0fe$3f8$1@newscl01ah.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2gv2e$s0n$1@newscl01ah.mathworks.com>...
> > "Matt J" wrote in message <j2duoh$qol$1@newscl01ah.mathworks.com>...
> > > "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2dtoj$n4c$1@newscl01ah.mathworks.com>...
> > > >
> > > > Thank you very much. I am sorry to bother you again. This hopefully will be the last question. how you would loop the code that you suggested below. For example how is it possible to change the positioning of grid1D inside the function? For example at iteration 1 H(grid1D, x(2),x(3),...,x(9))), then iteration 2 H(x(1), grid1D, x(3),...,x(9)) and so on... I tried many tricks but it didnt work. Thanks a lot for your advice on this. Best. S
> > > =========================
> > >
> > > You'd do something like the following:
> > >
> > > arguments=num2cell(x);
> > >
> > > for k=1:9 %loop over the 9 parameters
> > >
> > > arguments{k}=grid1D; %Or, make grid1D depend on k also
> > >
> > > [Hval,idx] = min( H(arguments{:}) ); %Do you understand this. See help docs about comma-separated lists if not!!!
> > >
> > > arguments{k}=grid1D(idx);
> > >
> > > end
> > >
> > > x=cell2mat(arguments);
> >
> > Hi Matt
> >
> > Thanks for the code it is really helpful. Correct me if I am wrong but the code that you suggested only allow to loop the positioning of the grid1D in the function "H" but doesnt allow the evaluation of the function at each grid point in grid1D? My aim is to create a second loop (inside the loop of "k=1:9") where I can access the element of gridD1 and evaluate the function at each grid point.
> > The only problem is that I dont know how to access the element of the grid1D in the second for loop. For example,
> >
> > grid1D= -10:1:10;
> >
> > arguments=num2cell(x);
> >
> > for k=1:9
> >
> > arguments{k}=grid1D;
> >
> > v=length(grid1D)
> >
> > for i=1:v
> >
> >
> > %% in this loop I would like to access the elements of the grid1D and evaluate the function at each iteration. Usually to access the element of cell we do the following arguments{1}(1). But in this case it doesnt apply.
> >
>
> Why not access them as grid1D(i)?
>
> In any case, you should only do this if you cannot vectorize the evaluation of H. I was under the impression that when you evaluate
> H(w,x,y,z,...) the argument list w,x,y,z, etc... can consist of scalars and 1 vector

Hi Matt

No unfortunately I cannot vectorize "H". The argument list of H is only scalars. The reason why I am putting a grid there is because I would like to loop over the value of grid1 by keeping the other parameters fixed. The aim is that:

1) in the small loop I can evaluate the function by trying all the value in grid1 by keeping the other parameters fixed,

2) When the small loop finishes, the grid will replace the second argument of function "H"

3) loop again over the value of grid 1 and so on.

How do you think I would set up this structure? Thanks a lot for your advice on this

Best

S

Subject: 9 dimensional grid

From: Matt J

Date: 18 Aug, 2011 11:38:08

Message: 15 of 15

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message <j2im34$anc$1@newscl01ah.mathworks.com>...
>
> How do you think I would set up this structure? Thanks a lot for your advice on this
=============

Same approach as before, except in the inner loop you will have

for i=1:v

  arguments{k}=grid1D(i);

end

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