[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Wish to avoid multiple for loops, but don't know how to do it
From: |
Ben Abbott |
Subject: |
Re: Wish to avoid multiple for loops, but don't know how to do it |
Date: |
Thu, 19 May 2011 19:54:38 -0400 |
On May 19, 2011, at 4:05 PM, Jordi GutiƩrrez Hermoso wrote:
> On 19 May 2011 12:43, clustro <address@hidden> wrote:
>> for i = 1:N
>> for j = 1:N
>> for k = 1:N
>> for l = 1:N
>> xPoint = [x(i) x(j) x(k) x(l)]';
>> fEval_x = colville(xPoint);
>> if fEval_x < fmin
>> fmin = fEval_x;
>> xmin = xPoint;
>> endif
>> endfor
>> endfor
>> endfor
>> endfor
>>
>> Where colville() is an optimization toy function.
>>
>> Does anyone have a suggestion on how to avoid 20 nested for loops when
>> trying to scale this algorithm up to higher dimensions?
>
> I don't understand the full extent of your problem, you basically want
> the minimum over the n-fold tensor product of a vector?
>
> You might be able to do this with arrayfun, although I don't
> immediately see how to avoid the actual tensoring.
>
> - Jordi G. H.
I'm not sure how to use arrayfun(), but using cellfun() ...
N = 4;
Nf = factorial (N);
input_permutations = mat2cell (perms (4), ones (Nf, 1), 4);
opt_fun = @(x) colville (x{:});
fEval_x = cellfun (opt_fun, input_permutations);
x_min = input_permutations (fEval_x == min (fEval_x));
The resulting x_min will be a cell array whose length is equal to the number of
results equal to the minimum value.
Ben
p.s. I haven't tested this code, so there may be some typo.