Function handle for combinations of calculations

조회 수: 1 (최근 30일)
Baium
Baium 2019년 10월 8일
편집: Matt J 2019년 10월 8일
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