How can I count special type of combinations with matlab?
Show older comments
Hi: I am a computer engineer and I am trying to count the following type of combinations with an unknown built-in function in Matlab: Let K={1,2,...,20} be the set of the first 20 integers.Consider all combinations of 9 elements selected from the set K.How many of these combinations contain elements that differ by 3? For example (7,10,11,12,14,17,18,19,20)has elements 7 and 10 whose difference is 3 while the combination (1,2,6,7,8,12,13,14,19) has no elements with difference 3. Using Matlab,how many combinations are there that contain elements that differ by 3? Thank you in advance.
2 Comments
Sean de Wolski
on 25 Aug 2011
This sounds a lot like homework. What have you tried so far?
Fangjun Jiang
on 25 Aug 2011
Any difference or just the difference between two neighbours? Does [7 8 9 10 ....] counts because there is 10-7=3?
Answers (1)
Sean de Wolski
on 25 Aug 2011
0 votes
Anyway, darkness golf:
- 40 characters: to return the matrix of combos without suppressing output and assigning to 'ans'
- 36 characters: to get the number of combinations without suppressing output and assigning to 'ans'
8 Comments
Fangjun Jiang
on 25 Aug 2011
If just for neighboring difference, I got 36 characters. The result is 120616. What's yours?
Sean de Wolski
on 25 Aug 2011
yes, I assumed sorted neighbors and I also have 120616. 36!! huummmphhh
Fangjun Jiang
on 25 Aug 2011
Well, I didn't sort. Why do you need to sort?
Sean de Wolski
on 25 Aug 2011
Ahh, counting just the number, not actually returning the matrix, I get 36 (not setting it to a variable or terminating output)
Sean de Wolski
on 25 Aug 2011
Not sorting, just saying neighbors in a sorted list; i.e. combinations are sorted and can not be any permutation.
Sean de Wolski
on 25 Aug 2011
Which brings up the campfire question us nerds had the other night: why are "combination" locks called "combination locks" and not "permutation locks" since that's what they really are?
Fangjun Jiang
on 25 Aug 2011
I think we got the same solution. "Combination lock" is just easy for average folks.
Walter Roberson
on 25 Aug 2011
Hmmm -- some "combination locks" allow repeated numbers (especially the disc-type line-up-the-numbers locks). I'm not sure that "permutation" would be appropriate either.
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!