All possible combination of variables in equation to give best result.
1 次查看(过去 30 天)
显示 更早的评论
Suppose I have A+B = answer and A ranges from 1 to 10 and B ranges from 1 to 10. How do I use matlab to solve for all values of A and B and display the result I want.
For eg: If I want the result to be 19 It must try A=1, B=1; A=2, B=1 ........... and finally A=10, B=9 or B= 10 and A=9 and so on to give the answer 19 (value required). Matlab finally must display various combinations of A, B that gives the result I require.
Note that the range can be long.
0 个评论
回答(1 个)
Jon
2020-2-10
Assuming A and B are both column vectors You can create a table with all possible sums using
total = A + B'
You can then find the elements of A and B that produce a desired sum, let's call it C, using
[idxA,idxB] = find(total == C)
You could then display the combinations using
result = [A(idxA) B(idxB)]
2 个评论
Stephen23
2020-2-10
It is a good habit to use element-wise transpose
B.'
Jon
2020-2-11
I can definitely see that you have to be careful about this when doing operations involving complex values. For situations where all of the variables are real somehow the B.' seems less readable than just using B' and if the imaginary part is zero (values are real) the answer will of course be the same. So for situations clearly involving only real numbers I feel it is preferable to have the improved readability. I can also see your view that you just like to have the one rule and stick with it.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!