I am trying to create a matrix that list all combinations possible under a certain condition. Right now I am creating all combinations and then filtering but the matrix of combination is too large.
I want to keep only when
Thank you
I am putting an example with smaller vector size
x = [0 0.25 0.5 0.75 1]
x_1 = [0 0.5 1]
x_2 = [0 0.5 1]

4 件のコメント

Torsten
Torsten 2023 年 2 月 9 日
I don't understand your example.
Les Beckham
Les Beckham 2023 年 2 月 9 日
Combinations of what?
Jan
Jan 2023 年 2 月 9 日
I don't either. What are the inputs you want to combine?
Nicolas Leport
Nicolas Leport 2023 年 2 月 9 日
Sorry I wasn't clear
I want to have all to possible combinations of x, x_1 and x_2 with the condition
I want to end with something like this
Hope it makes more sense
0 0 0
0.25 0 0.5
0.25 0.5 0
0.5 0 1
0.5 0.5 0.5
0.5 1 0
0.75 0.5 1
0.75 1 0.5
1 1 1

サインインしてコメントする。

 採用された回答

Voss
Voss 2023 年 2 月 9 日

1 投票

Maybe something like this?
x = [0 0.25 0.5 0.75 1];
x_1 = [0 0.5 1];
x_2 = [0 0.5 1];
x_12 = (x_1.'+x_2)/2;
[ism,idx] = ismember(x_12,x);
[r,c] = find(ism);
result = [x_1(r); x_2(c); x(idx(ism))].'
result = 9×3
0 0 0 0.5000 0 0.2500 1.0000 0 0.5000 0 0.5000 0.2500 0.5000 0.5000 0.5000 1.0000 0.5000 0.7500 0 1.0000 0.5000 0.5000 1.0000 0.7500 1.0000 1.0000 1.0000

7 件のコメント

Nicolas Leport
Nicolas Leport 2023 年 2 月 9 日
Hopefully yes, just with the the 3rd column in an increasing order
Voss
Voss 2023 年 2 月 9 日
x = [0 0.25 0.5 0.75 1];
x_1 = [0 0.5 1];
x_2 = [0 0.5 1];
x_12 = (x_1.'+x_2)/2;
[ism,idx] = ismember(x_12,x);
[r,c] = find(ism);
result = [x_1(r); x_2(c); x(idx(ism))].';
% sort on 3rd column:
[~,idx] = sort(result(:,3));
result = result(idx,:)
result = 9×3
0 0 0 0.5000 0 0.2500 0 0.5000 0.2500 1.0000 0 0.5000 0.5000 0.5000 0.5000 0 1.0000 0.5000 1.0000 0.5000 0.7500 0.5000 1.0000 0.7500 1.0000 1.0000 1.0000
Nicolas Leport
Nicolas Leport 2023 年 2 月 9 日
Thank you!
Voss
Voss 2023 年 2 月 9 日
You're welcome!
Nicolas Leport
Nicolas Leport 2023 年 2 月 9 日
How would you modify it if I wanted to add a x_3 and now (x1+x2+x3)/3=x ?
Walter Roberson
Walter Roberson 2023 年 2 月 9 日
Voss's solution creates all combinations and filters them, which you had said gets too large for your purposes.
ismember with floating point is risky. This example with exact fractions of powers of 2 gets away with it, but you would have problems with 0.1 and 0.2 for example
Nicolas Leport
Nicolas Leport 2023 年 2 月 9 日
I actually changed x, x1, x2 to be whole number and then divide the result matrix by the number of data points. It works fine. But I don't know how to modify it to do it with x, x1, x2 and x3

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by