Since you have mentioned that you are new to parallel programming in MATLAB, I suggest going through the following resources:
What Is Parallel Computing? - https://www.mathworks.com/help/parallel-computing/what-is-parallel-computing.html
Choose a Parallel Computing Solution - https://www.mathworks.com/help/parallel-computing/choosing-a-parallel-computing-solution.html
Regarding the issue at hand, I suggest using "parfor" from the parallel computing toolbox to execute the above loop using multiple workers. The first example in the "Convert a for-Loop Into a parfor-Loop" section of the below document illustrates how a series loop operation may be parallelized.
Interactively Run a Loop in Parallel Using parfor - https://www.mathworks.com/help/parallel-computing/interactively-run-a-loop-in-parallel.html
In this case, the variables "Y_record_generation" and "record_generation" are being shared across all iterations. You will have to make sure that each iteration is totally independent of all the others.
One way to do this could be to make modifcations to your code as shown below:
parfor n=1:nbr_individus
Y_record_generation(n) = simulation(population(n,:),P);
record_generation(n) = 2*norm(Y(end,2:4)'-[P.tXo;P.tyo;terrain(P.tXo,P.tyo)])^2+0.2*Y(end,1)^2;
end
You can then calculate the "Y_record_generation" value corresponding to the minimum "record_generation" value as:
Y_record_generation = Y_record_generation(record_generation == min(record_generation));
For further reference, consider going through the following document:
Documentation for "parfor" - https://www.mathworks.com/help/parallel-computing/parfor.html
There are certain caveats to using "parfor" which are mentioned in the "Tips" section of the documentation provided above.