How to use matlab to write code with multiple loops?
Show older comments
I want to use genetic algorithm to solve such a problem, but I don't know how to construct the objective function and constraint conditions

My code is as follows. I have many bugs due to my poor level. I also hope that the leaders can give me some advice.
My main problem is that I don't know how to write the objective function and constraint conditions. Please give me some advice
2 Comments
dpb
on 5 Mar 2023
If P and x are both arrays, then "the MATLAB way" is to use vectorized operations --
y=sum(P.*x,'all)';
I don't follow the intent with the second; the index 101 would be outside the array bounds of 1:100 given; and doesn't seem to match up with the handwritten equations anyway...
The way I like to think about nested for loop is this:
Suppose, you have a 2D matrix and you need to double the value for every element (you really don't need a for loop to do that, but this is an easy example to understand the basis of it).
A = rand(5,6)
for i = 1:size(A,1) % it is ready to change the values for every elements of row 1
for j = 1:size(A,2) % when i = 1, it will change the values for all columns of row 1 and the loop continues
A(i,j) = 2*A(i,j);
end
end
A
Answers (0)
Categories
Find more on Loops and Conditional Statements 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!