Function handle for combinations of calculations

1 visualización (últimos 30 días)
Baium
Baium el 8 de Oct. de 2019
Editada: Matt J el 8 de Oct. de 2019
Hello,
I am trying to create (a) function handle(s) that support every possible combination of addition and subtraction among the specified variables.
Assume we have @(a, b, x, y, z), an exemplary subset of all possible combinations would be:
a - (b + x + y + z)
z - (a + b + x + y)
x - (a + b + y + z)
(x + a + z) - (b + y)
(z + a + b + y) - x
(a + b) - (x + y + z)
... (and many more)
As you can tell the combinations should solely support addition and subtraction, with any possible number of elements on either side of the equation.
Can anyone guide me through the function calls that would support this? I have thought of using either perms or combnk and use 1s and -1s so as to apply "weights" and use a simple addition instead, with the sign of each weight dictating the addition/subtraction, but I am not entirely sure how to process this.
Any help will be appreciated

Respuesta aceptada

Matt J
Matt J el 8 de Oct. de 2019
Editada: Matt J el 8 de Oct. de 2019
One way:
N=5;
[G{1:N}]=ndgrid([-1,1]);
G=reshape(cat(N+1,G{:}) , [],N);
and now the combinations are given by
G*[a;b;x;y;z]

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by