All possible combination of variables in equation to give best result.

5 ビュー (過去 30 日間)
Lakshmikruthiga Ponnusamy
Lakshmikruthiga Ponnusamy 2020 年 2 月 10 日
コメント済み: Jon 2020 年 2 月 11 日
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.

回答 (1 件)

Jon
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
Stephen23 2020 年 2 月 10 日
It is a good habit to use element-wise transpose
B.'
unless you explicitly need the complex conjugate transpose.
Jon
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by