Combinations with a condition
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nicolas Leport
el 9 de Feb. de 2023
Comentada: Nicolas Leport
el 9 de Feb. de 2023
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 comentarios
Respuesta aceptada
Voss
el 9 de Feb. de 2023
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))].'
7 comentarios
Walter Roberson
el 9 de Feb. de 2023
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
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!