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

4 vues (au cours des 30 derniers jours)
Lakshmikruthiga Ponnusamy
Lakshmikruthiga Ponnusamy le 10 Fév 2020
Commenté : Jon le 11 Fév 2020
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.

Réponses (1)

Jon
Jon le 10 Fév 2020
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 commentaires
Stephen23
Stephen23 le 10 Fév 2020
It is a good habit to use element-wise transpose
B.'
unless you explicitly need the complex conjugate transpose.
Jon
Jon le 11 Fév 2020
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by