How to use matlab to write code with multiple loops?
显示 更早的评论
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 个评论
dpb
2023-3-5
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
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!