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);
One line :)
I finally got it!
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
Add a test for multiple matches in the vector.
if true(find(b == a))
y = 1
else
y = 0
end
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
There is a pre-made function for this.
y = ismember(a,b)
Thanks! Have updated tests.
423 Solvers
The Answer to Life, the Universe, and Everything
388 Solvers
494 Solvers
393 Solvers
484 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!