Problem 838. Check if number exists in vector
Return 1 if number a exists in vector b otherwise return 0.
a = 3; b = [1,2,4];
Returns 0.
a = 3; b = [1,2,3];
Returns 1.
Solution Stats
Problem Comments
-
7 Comments
Add test vector a = -12;
b = [1,3,4,5,6,7,8,-12,2]; and rescore.
Better is add a=-randi(16); b= [1 2 3 a];
These will eliminate answers like #6.
Tests allow incorrect solution to pass:
function y = existsInVector(a,b)
y=0
for i = 1:numel(b);
if i==a
y=1
break
end
end
end
good
that was fun, took me a couple minutes
y = sum(b == a);
Not too bad
yay
Solution Comments
Show commentsProblem Recent Solvers9865
Suggested Problems
-
Back to basics 4 - Search Path
359 Solvers
-
Make an awesome ramp for a tiny motorcycle stuntman
604 Solvers
-
Longest run of consecutive numbers
4834 Solvers
-
Relative ratio of "1" in binary number
1093 Solvers
-
Matlab Basics - Convert a row vector to a column vector
595 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!