Clear Filters
Clear Filters

Nested For Loop Without Repetitions! PLEASE HELP

4 views (last 30 days)
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!

Accepted Answer

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

Sign in to comment.

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!