how can I make the code generating all the possible combinations of integer numbers more efficient
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hugo
el 27 de En. de 2022
Respondida: Benjamin Thompson
el 27 de En. de 2022
Hi,
I would like to know how I can modify the following code, as to make it more efficient. This code writes the matrix total, which has all the possible combinations of 4 and 5 numbers, considering all the integer numbers from 1 to 10. I thank you in advance.
v = 1:1:10;
sens5 = nchoosek(v,5)
sens5(:,6)=0
sens5(:,7)=0
sens5(:,8)=0
sens5(:,9)=0
sens5(:,10)=
sens4 = nchoosek(v,4)
sens4(:,5)=0
sens4(:,6)=0
sens4(:,7)=0
sens4(:,8)=0
sens4(:,9)=0
sens4(:,10)=0
total=[sens4; sens5]
0 comentarios
Respuesta aceptada
Benjamin Thompson
el 27 de En. de 2022
This runs very fast. But if you are scaling up to a bigger problem, you can probably precompute the value of the factorial operation, just look at the mathematical definition of nchoosek. Then if you are sequentially calculating nchoosek with increasing n, the math of calculating it for n+1 can be done using the previous nchoosek.
There is also parfor, or see if nchoosek can be run on a GPU with a gpuArray input if you have a good GPU.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Number Theory en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!