combinations

Most optimal coin fit for a given sum of money

安稳与你 提交于 2019-12-20 02:29:17
问题 How would you reach a given sum in the most optimal manner possible given a set of coins ? Let's say that in this case we have random numbers of 1, 5, 10, 20 and 50 cent coins with the biggest coins getting the priority. My first intuition would be to use all the biggest coins possible to fit and then use up the next smallest coin in value if the sum is exceeded. Would this do or are there any shortfalls to this approach ? Are there any more efficient approaches ? 回答1: There are shortfalls to

Select a subset of combinations

荒凉一梦 提交于 2019-12-20 01:58:19
问题 Suppose that I have a 20 X 5 matrix, I would like to select subsets of the matrix and do some computation with them. Further suppose that each sub-matrix is 7 X 5. I could of course do ncomb <- combn(20, 7) which gives me all possible combinations of 7 row indices, and I can use these to obtain sub-matrices. But with a small, 20 X 5 matrix, there are already 77520 possible combination. So I would like to instead randomly sample some of the combinations, e.g., 5000 of them. One possibility is

Product between all combinations of a vector's elements

戏子无情 提交于 2019-12-19 21:25:39
问题 Suppose I have a vector c(1, 2, 3, 4) with no duplicated values. I need a vector c(1 * 2, 1 * 3, 1 * 4, 2 * 3, 2 * 4, 3 * 4) , so the multiplication is done in all possible combinations of this vector's values. Is there a way of doing that? Thanks in advance! 回答1: We can use combn with anonymous function call combn(vec, 2, FUN = function(x) x[1] * x[2]) #[1] 2 3 4 6 8 12 data vec <- 1:4 回答2: This is fun enough. I thought combn(1:4, 2, "*") would be the simplest solution but it actually does

intelligently generating combinations of combinations

一世执手 提交于 2019-12-19 21:04:08
问题 Let's say I have a class of 30 students and want generate every possible way in which they can be partitioned into groups of 5 (order is irrelevant). I know how to find all the combinations of students to form one group individually (http://www.merriampark.com/comb.htm). By using that iterator and some recursion, I can find PERMUTATIONS of the possible group combinations. However, order in which the groups are selected isn't relevant and I'd like to minimize my execution time. So how do I

How to assign a counter to a specific subset of a data.frame which is defined by a factor combination?

寵の児 提交于 2019-12-19 17:15:08
问题 My question is: I have a data frame with some factor variables. I now want to assign a new vector to this data frame, which creates an index for each subset of those factor variables. data <-data.frame(fac1=factor(rep(1:2,5)), fac2=sample(letters[1:3],10,rep=T)) Gives me something like: fac1 fac2 1 1 a 2 2 c 3 1 b 4 2 a 5 1 c 6 2 b 7 1 a 8 2 a 9 1 b 10 2 c And what I want is a combination counter which counts the occurrence of each factor combination. Like this fac1 fac2 counter 1 1 a 1 2 2 c

How to assign a counter to a specific subset of a data.frame which is defined by a factor combination?

泄露秘密 提交于 2019-12-19 17:14:17
问题 My question is: I have a data frame with some factor variables. I now want to assign a new vector to this data frame, which creates an index for each subset of those factor variables. data <-data.frame(fac1=factor(rep(1:2,5)), fac2=sample(letters[1:3],10,rep=T)) Gives me something like: fac1 fac2 1 1 a 2 2 c 3 1 b 4 2 a 5 1 c 6 2 b 7 1 a 8 2 a 9 1 b 10 2 c And what I want is a combination counter which counts the occurrence of each factor combination. Like this fac1 fac2 counter 1 1 a 1 2 2 c

All possible combinations for 3 digits of list never the same

眉间皱痕 提交于 2019-12-19 12:00:34
问题 I have a list that looks like: A B C D E F G How do I solve this to find all combinations for 3 digits. The same letter cannot be used in same row. ABC ABD ABE ABF ABG AGB E.g something like...: x = ['a','b','c','d','e'] n = 3 import itertools aa = [list(comb) for i in range(1, n+2) for comb in itertools.combinations(x, i)] print(aa) This does not give desired input: [['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e'], ['c'

How to choose identical values in a cell array with constraints using Matlab?

大城市里の小女人 提交于 2019-12-19 11:56:28
问题 If I have a 4x1 Cell structure with that represents a=[A1 A2 A3 A4] : a=cell(4,1) a{1}=[1 3 1 0] a{2}=[3 3 3 3] a{3}=[3 2 3 2] a{4}=[3 3 3 2] B=[1 1 1 2]; %priority I would like to do the following : Pick cells that correspond to priority B=[1 1 1 2] (where B=1 is highest priority and A=3 ) Which means, find any cell that begins with [3 3 3 #], where all their priority is 1's in B . ideal answer should be : a{2} = [3 3 3 3] and a{4} = [3,3,3,2] My try is to add this : [P arrayind]=min(B) %

Making Combinations (Python)

寵の児 提交于 2019-12-19 11:48:13
问题 In Python, is there a better way to get the set of combinations of n elements from a k-element set than nested for loops or list comprehensions? For example, say from the set [1,2,3,4,5,6] I want to get [(1,2),(1,3),(1,4),(1,5),(1,6),(2,3),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6),(4,5),(4,6),(5,6)]. Is there a better of of making it than nums=[1,2,3,4,5,6] doubles=[] for a in nums: for b in nums[a+1:] doubles.append((a,b)) ? It's okay if the elements of the list we end up with are sets, tuples, or

Complete set of combinations combining 3 set

前提是你 提交于 2019-12-19 11:25:41
问题 I need to generate the complete set of combinations obtained combining three different subset: Set 1 : choosing any 4 numbers from a vector of 13 elements. Set 2 : choosing any 2 numbers from a vector of 3 elements. Set 3 : choosing any 2 numbers from a vector of 9 elements. Example: sample 3 from vector of 4 (H=3 and L=4) for the Set A, H=2 L=3 for the Set B and H=2 L=4 for the Set B: 4 4 4 3 4 4 3 3 4 3 3 3 2 4 4 2 3 4 2 3 3 2 2 4 Set A = 2 2 3 2 2 2 1 4 4 1 3 4 1 3 3 1 2 4 1 2 3 1 2 2 1 1