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!