Problem 43. Subset Sum
Solution Stats
Problem Comments

4 Comments
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.
Solution Comments

2 Comments
why here comes an error as:
idx=logical(de2bi(todecimal(1:(2^l1)),16));
when I wanna use command:
idx=logical(de2bi(todecimal(1:(2^l1)),16));
my local matlab 2019b function well, but online project confuze me.
And I found that:
Others solution used dec2bin function, which has matlab document as the de2bi function, work out the same way, but maybe the compile problem here makes the different result of online systeerm to local solftware.

1 Comment
why this isn't working here?
it works on my laptop;
function ind = subset_sum(v,n)
k=dec2bin(0:2^length(v)1);
b=k.*v;
l=find(sum(b')==n);
m=k(l,:).*(1:length(v));
ind=m(m>0);
end

1 Comment
nchoosek function is fantastic!

2 Comments
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.]

1 Comment
While I think I should have a rest and think more....

2 Comments

2 Comments
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.

1 Comment
great solution

2 Comments
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.

1 Comment
artful solution.I can't think it out myself

2 Comments
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.

1 Comment
why???

1 Comment
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.}

1 Comment
well, I guess with Cody's way of scoring this would work, but honestly, a bit risky to use in "real life"

1 Comment
Buggy, mine is empty but with size, and doesn't get right answer.

1 Comment
only works for v > 0
Problem Recent Solvers1348
Suggested Problems

Return the largest number that is adjacent to a zero
4481 Solvers

417 Solvers

1140 Solvers

368 Solvers

4760 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!