Clear Filters
Clear Filters

for loops auto nidification problem

21 views (last 30 days)
Emiliano Rosso
Emiliano Rosso on 13 May 2024
Answered: Zinea on 17 May 2024 at 5:11
I have a function y= f(x) .
x is 10 x n and y= 1 x n
a performance function myperf = perf(y) gives a 1 x 1 value.
a target value mytar must be reached.
if mytar is not reached I must reiterate the process in this way :
for i=1:10
x=y % it must be 10 x n
and finally ...
if myperf = perf(y) > mytar
I must continue to the next iteration and rebuild the code for 2 for loops and so on.
I can't known how much iteration I'll need.
x dimension is 10^niteration x n ( 10 ,100 ,1000 ... 10^niteration)
I must pay attention to use intermediate results to not reinitialize the algorithm every time and loose resources.
What type of architecture cold I use ?
The most symple and understandable ?
Is there a way for code autogeneration ?
Thanks !

Answers (1)

Zinea on 17 May 2024 at 5:11
A while loop or a recursive function can be used that keeps track of the current iteration and dynamically adjusts the size of “x” based on the iteration count. Here is an outline of the implementation using a while loop, assuming “f(x)” and “perf(y)” are already defined:
function main_algorithm(initial_x, n, mytar)
iteration = 0; % Initialize iteration count
x = initial_x; % Initial x, should be 10 x n for the first iteration
while true
iteration = iteration + 1; % Increment iteration count
y = compute_y(x, n); % Compute y for the current iteration
myperf = perf(y); % Evaluate performance
if myperf > mytar
break; % Exit loop if target performance is achieved
x = prepare_next_x(y, iteration, n); % Prepare x for the next iteration
function y = compute_y(x, n)
y = zeros(1, n); % Initialize y
for i = 1:length(x)
y(i) = f(x(i)); % Assuming f(x) is defined elsewhere
function x = prepare_next_x(y, iteration, n)
% Dynamically resize x for the next iteration based on the current iteration
newSize = 10^iteration * n; % Calculate new size for x
x = repmat(y, 1, newSize / length(y)); % Repeat y to fill the new size of x
To enhance the efficiency of the problem at hand, the following approaches could be considered:
  1. Vectorization: Whenever possible, transform the loop that computes “y=f(x)” for each element of “x” into a vectorized operation. If “f(x)” is a function that can operate on vectors, all the values of “y” can be computed in a single line without an explicit loop, which is much faster.
  2. Parallel computing: For very large “x” vectors, and if “f(x)” computations are independent from one another, MATLAB’s Parallel Computing Toolbox can be used to distribute the computation of “y” across multiple cores or even GPUs if “f(x)” is compatible with GPU acceleration.
  3. Efficient memory management: Preallocate arrays whenever possible and use “single” instead of “double” precision if the decreased precision does not significantly impact the results, to halve the memory usage.
x = zeros(1, newSize, 'single'); % Preallocate with single precision
4. Adaptive algorithm adjustment: If certain patterns are observed in the relationship between ‘x, “y, and “perf(y)”, the algorithm might be written in a way that it adapts dynamically. For example, if improvements in performance diminish with each iteration, a different stopping criterion may be implemented instead of simply checking “perf(y) > mytar”.
5.Utilizing MATLAB Toolboxes: MATLAB’s toolboxes offer optimized functions and algorithms to compute “f(x)” and “perf(y)” more efficiently. For instance, the Optimization Toolbox could provide methods to optimize the input “x” directly based on “perf(y)”, leading to early convergence to “mytar”.
6. Saving intermediate results: For long-running results, consider saving intermediate results to disk. This can be a safeguard against data loss from unexpected interruptions by providing checkpoints from which the algorithm can be restarted without losing all progress.
You may refer to the following links to documentation for more information:
Hope this helps!


Find more on Startup and Shutdown in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!