Function handle for combinations of calculations

1 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2019-10-8
编辑:Matt J 2019-10-8
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]

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by