Nested For Loop Without Repetitions! PLEASE HELP

4 次查看(过去 30 天)
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!

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by