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

1 visualización (últimos 30 días)
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.

Respuestas (1)

Jon
Jon el 10 de Feb. de 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 comentarios
Jon
Jon el 11 de Feb. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by