Problem 568. Number of 1s in a binary string
Solution Stats
Problem Comments
-
3 Comments
Something weird is happening
When I submit the size of my code is -1 and it fails the test suite when it seems to work fine in MATLAB desktop.
would you please let me know why
sum(x) creates error?
Cool problem!
Solution Comments
-
1 Comment
cool!
-
1 Comment
bruv
-
1 Comment
good problem
-
2 Comments
Good problem helps understand how to use binary better
good problem
-
1 Comment
concepts matter
-
1 Comment
challenging!
-
1 Comment
easy
-
1 Comment
Interesting problem
-
1 Comment
ASCII
-
1 Comment
Can someone check this code?
function y = one(x)
split1= num2cell(x)
sum=0;
for i=1:length(split1)
if split1(i)=='1'
sum=sum+1
else
sum=0
i=i+1
y = sum;
end
-
1 Comment
What's wrong with this problem!!
-
2 Comments
used y=y+str2double(x(i)) instead of y=y+str2double(x(i)) and it worked just fine
function y = one(x)
y1=[];
for a=x(1:end)
y1 = [y1,str2num(a)];
end
y=sum(y1);
end
-
1 Comment
y=0;
for index=1:length(x)
y = y + str2num(x(index));
end
-
3 Comments
y = sum(x=='1');
Well, well! Here he/she is as well, great job continuing your copy and paste of the cheap and meaningless trick you prick!
In case you didn't know how to do it and wanted to know how -which somehow I doubt it for some reason!, but anyhow, here is one of MANY ways to do it:
function x=one(x),x=count(x,'1');end
y = length(find(x=='1'));
-
2 Comments
Why doesn't this solution work? Works just fine in my desktop program...
Because you compare a char Array to a double. Try (x=='1').
Problem Recent Solvers6538
Suggested Problems
-
2902 Solvers
-
"Low : High - Low : High - Turn around " -- Create a subindices vector
500 Solvers
-
444 Solvers
-
470 Solvers
-
433 Solvers
More from this Author1
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!