フィルターのクリア

Function handle for combinations of calculations

2 ビュー (過去 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 Center および File Exchange2-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