Given a vector v of integers and an integer n, return the the indices of v (as a row vector in ascending order) that sum to n. If there is no subset in v that sums to n, return an empty matrix []. You can assume that the answer will always be unique.
Example:
>> v = [2, 3, 5]; >> n = 8; >> subset_sum(v, n) ans = 2 3
The combntns(eg.combntns(some_vector,i)) function throws:
Error: Undefined function 'combntns' for input arguments of type 'double'
But on my machine it takes i as a double. It even throws it if I cast i to int8.
Use nchoosek instead of combnts or combnk
At first I thought that it was quite difficult to look for the sets of elements of any size (sets of 1 element, 2 elements and so on), but then I realised that any selection of the vector elements correspond to a binary code ('1' for selecting that element and '0' for not selecting it). To select all possible elements combinations I only need to use the binary code from 1 to 2^(1+length(v))-1.
At first I thought that it was quite difficult to look for the sets of elements of any size (sets of 1 element, 2 elements and so on), but then I realised that any selection of the vector elements correspond to a binary code ('1' for selecting that element and '0' for not selecting it). To select all possible elements combinations I only need to use the binary code from 1 to 2^(1+length(v))-1.
nchoosek function is fantastic!
So apparently this is failing because my empty matrix is
1×0 (size 0) double
but Test Case 3 requires an empty matrix that is
0×0 (size 0) double
Hmmmm....
So apparently this is failing because my empty matrix is 1×0 (size 0) double but Test Case 3 requires an empty matrix that is 0×0 (size 0) double Hmmmm....
[Subsequently amended to pass the test suite as Solution 1163376.]
While I think I should have a rest and think more....
What am I doing wrong? If I enter the code on my computer, I am getting the correct solutions for the first and last case. I even cleared all other variables from the workspace.
Apparently I was using intersect wrong.
great solution
Why? I think my solution is right, but it always gives me a 'Fail'??? Can anyone help?
Take a look at your error messages. They are telling you that the function "combntns" is undefined. So it appears that you have this function on your computer, but it is not part of MATLAB and so Cody doesn't know what it is.
artful solution.I can't think it out myself
Is this a bug in MATLAB? v(c) == v(c') !!!
I mean, v(c) == v(c') if c is a vector. This is not true for a matrix c.
why???
Despite the warning, I'm stoked it worked.
{Warning: Concatenation involves an empty array with an
incorrect number of rows.
This may not be allowed in a future release.}
well, I guess with Cody's way of scoring this would work, but honestly, a bit risky to use in "real life"
Buggy, mine is empty but with size, and doesn't get right answer.
only works for v > 0
21072 Solvers
587 Solvers
Determine Whether an array is empty
544 Solvers
Test if two numbers have the same digits
147 Solvers
144 Solvers