Problem 838. Check if number exists in vector
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
-
1 Comment
good problem
-
1 Comment
this took me while good problem!
-
2 Comments
One line :)
GOOD
-
1 Comment
I finally got it!
-
2 Comments
Can anyone tell me what's wrong in this code as I am getting the desired result in my laptop?
function y = existsInVector(a,b)
for i=1:length(b)
if a==b(i)
y=1
elseif
i=i+1
if i==3
y=0
end
end
end
end
u have finished just theexample
function y = existsInVector(a,b)
y=1-isempty(find(a==b));
end
-
1 Comment
Add a test for multiple matches in the vector.
-
1 Comment
Leading solutions are still broken
-
1 Comment
if true(find(b == a))
y = 1
else
y = 0
end
-
1 Comment
While evaluating the solution, the server encountered an error caused by temporary unavailability of MATLAB Service. Wait a few minutes for the MATLAB Service to return, and then rescore.
function y = existsInVector(a,b)
y=ismember(a,b);
end
-
1 Comment
There is a pre-made function for this.
y = ismember(a,b)
-
1 Comment
Thanks! Have updated tests.
Problem Recent Solvers7257
Suggested Problems
-
13956 Solvers
-
Find all elements less than 0 or greater than 10 and replace them with NaN
14187 Solvers
-
Remove any row in which a NaN appears
7578 Solvers
-
303 Solvers
-
Find nearest prime number less than input number
439 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!