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);
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.
use ismember(a,b)
y = ~isempty(b(b == a))
if true(find(b == a))
y = 1
else
y = 0
end
function y = existsInVector(a,b)
y = ismember(a,b)
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.
9844 Solvers
579 Solvers
Matrix indexing with two vectors of indices
485 Solvers
Find a subset that divides the vector into equal halves
332 Solvers
Find the sides of an isosceles triangle when given its area and height from its base to apex
450 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!