Nested For Loop Without Repetitions! PLEASE HELP

10 visualizaciones (últimos 30 días)
Austin Welch
Austin Welch el 29 de Jun. de 2018
Comentada: Austin Welch el 29 de Jun. de 2018
My problem is that I have a array of strings, which are names, that can vary in length depending on how many the user inputs. I am okay up until that point, but I also want to store every combination of six of the names in the array. I was using six nested loops that ran from one to the number of names so that it would run through all combinations of the names. The issues are that with that method, the same name can show up multiple times in a collection of six name, the same combination of people can show up just in a different order, and it seems to take a long time.
I was trying to get rid of the same name showing up in the same grouping of six people with an if statement, but it seems tedious and also adds time. There are normally around 20 people inputted so that would be 64,000,000 different combinations if I did not filter out the repeated names and repeated groups. This just takes a lot of time because I am also trying to add if statements for specific characteristics of the people, which again adds more time.
Anyways, I would greatly appreciate if someone had a efficient way to run through every combination of picking six people from the group with no repeated names in each group and no repeated groups overall.
I have been trying to figure this out for a long time now and just can't wrap my mind around it, so if someone knows an answer please help! Thanks so much!
  1 comentario
Walter Roberson
Walter Roberson el 29 de Jun. de 2018
There are 38760 combinations of 20 items taken 6 at a time.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Jun. de 2018
idx = nchoosek(1:length(YourCell), 6);
choosen = YourCell(idx);
  1 comentario
Austin Welch
Austin Welch el 29 de Jun. de 2018
Thank you so much I think this is exactly what I need! This is much faster and saves so much work!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by