How to find out the best combination of a given vector whose sum is closest to a given number

Tyelcie

My question is quite similar to this one: Find a subset from a set of integer whose sum is closest to a value

It discussed the algorithm only, but I want to solve it with R. I'm quite new to R and tried to work out a solution, but I wonder whether there is a more efficient way.

Here is my example:

# Define a vector, to findout a subset whose sum is closest to the reference number 20. 
A <- c(2,5,6,3,7)

# display all the possible combinations
y1 <- combn(A,1)
y2 <- combn(A,2)
y3 <- combn(A,3)
y4 <- combn(A,4)
y5 <- combn(A,5)
Y <- list(y1,y2,y3,y4,y5)

# calculate the distance to the reference number of each combination
s1 <- abs(apply(y1,2,sum)-20)
s2 <- abs(apply(y2,2,sum)-20)
s3 <- abs(apply(y3,2,sum)-20)
s4 <- abs(apply(y4,2,sum)-20)
s5 <- abs(apply(y5,2,sum)-20)
S <- list(s1,s2,s3,s4,s5)

# find the minimum difference
M <- sapply(S,FUN=function(x) list(which.min(x),min(x)))
Mm <- which.min(as.numeric(M[2,]))

# return the right combination
data.frame(Y[Mm])[as.numeric(M[,Mm[1]])]

so the answer is 2,5,6,7.

How can I refine this program? Especially the five combn()s and five apply()s, is there a way that can work them at once? I hope when A has more items in it, I can use length(A) to cover it.

Sotos

Here is another way to do it,

l1 <- sapply(seq_along(A), function(i) combn(A, i))
l2 <- sapply(l1, function(i) abs(colSums(i) - 20))

Filter(length, Map(function(x, y)x[,y], l1, sapply(l2, function(i) i == Reduce(min, l2))))
#[[1]]
#[1] 2 5 6 7

The last line uses Map to index l1 based on a logical list created by finding the minimum value from list l2.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

what can be the best algorithm to find numbers with only single set bit and whose sum is equal to given number?

From Dev

How to find a value in an array that is closest to a given number

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

Find permutations of a given set of numbers whose sum APPROACH a target number

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Java

How to find the number that best matches a given number

From Dev

how to find the length of the longest contiguous subarray whose sum is divisible by a given number

From Dev

Finding closest sum of numbers to a given number

From Dev

how do I find the closest value to a given number in an array?

From Dev

program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

From Dev

Given a float, find if in list or closest number to it

From Dev

Given a float, find if in list or closest number to it

From Dev

Find the closest higher number that has a given divisor

From Dev

SQL: Find closest number to given value with ties

From Dev

Find elements that sum to a given number

From Dev

how to find the minimum number of primatics that sum to a given number

From Dev

Given two sets of vectors, how do I find the closest vector in the second set for each vector in the first set?

From Dev

How to obtain a vector with the indexes of the elements given in a combination?

From Dev

How to obtain a vector with the indexes of the elements given in a combination?

From Dev

how to find the closest string for a given string with SQL

From Dev

how to find the closest string for a given string with SQL

From Dev

Find closest solution from a given vector for a linear equation in Matlab

From Dev

Number of Subarray whose sum greater than given value

From Dev

How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value

From Dev

Find the minimum set of integers whose sum is greater than a given integer

From Dev

sum of BITS is given find thar number

From Dev

How to find a range of given number?

From Dev

How do I find the closest n numbers to a given number at x distance from it?

From Dev

How do I find the closest n numbers to a given number at x distance from it?

Related Related

  1. 1

    what can be the best algorithm to find numbers with only single set bit and whose sum is equal to given number?

  2. 2

    How to find a value in an array that is closest to a given number

  3. 3

    Find numbers of subarray of an array whose sum is divided by given number

  4. 4

    Find permutations of a given set of numbers whose sum APPROACH a target number

  5. 5

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  6. 6

    How to find the number that best matches a given number

  7. 7

    how to find the length of the longest contiguous subarray whose sum is divisible by a given number

  8. 8

    Finding closest sum of numbers to a given number

  9. 9

    how do I find the closest value to a given number in an array?

  10. 10

    program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

  11. 11

    Given a float, find if in list or closest number to it

  12. 12

    Given a float, find if in list or closest number to it

  13. 13

    Find the closest higher number that has a given divisor

  14. 14

    SQL: Find closest number to given value with ties

  15. 15

    Find elements that sum to a given number

  16. 16

    how to find the minimum number of primatics that sum to a given number

  17. 17

    Given two sets of vectors, how do I find the closest vector in the second set for each vector in the first set?

  18. 18

    How to obtain a vector with the indexes of the elements given in a combination?

  19. 19

    How to obtain a vector with the indexes of the elements given in a combination?

  20. 20

    how to find the closest string for a given string with SQL

  21. 21

    how to find the closest string for a given string with SQL

  22. 22

    Find closest solution from a given vector for a linear equation in Matlab

  23. 23

    Number of Subarray whose sum greater than given value

  24. 24

    How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value

  25. 25

    Find the minimum set of integers whose sum is greater than a given integer

  26. 26

    sum of BITS is given find thar number

  27. 27

    How to find a range of given number?

  28. 28

    How do I find the closest n numbers to a given number at x distance from it?

  29. 29

    How do I find the closest n numbers to a given number at x distance from it?

HotTag

Archive