|
"Erik" <emiehling@gmail.com> wrote in message <h6hv5n$abp$1@fred.mathworks.com>...
> Hi,
>
> I have an algorithm that involves sweeping over all possible values of 4 different parameters. The logical solution would be to use a for loop; however, this is kind of slow in cases.
>
> My first thought was to use meshgrid, but it only seems to accept a maximum of 3 parameters, i.e. [x,y,z] = meshgrid(1:10,1:10,1:10). The moment I try to add a fourth parameter, it gives me an error.
>
> My next idea was to use ndgrid. However, this is giving me many more cases than I need for a reason I am not sure of. For example,
>
> vec = 1:20;
> for p = vec
> for q = vec
> for r = vec
> for s = vec
> ... blah ...
> end
> end
> end
> end
>
> gives a different result than,
>
> [p,q,r,s] = ndgrid(vec,vec,vec,vec)
> ... blah ...
>
Because you've invoked ndgrid such that p is your fastest varying variable, whereas in your for loop it is the slowest. Doing this instead should bring them into consistency:
[s,r,q,p] = ndgrid(vec,vec,vec,vec)
|