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
- 
		9 Comments
		    Show
		    6 older comments
		  
		  
		Richard Zapor
    	on 17 Jul 2012
	
	
  	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.
		frank mcnally
    	on 15 Aug 2019
	
	
  	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
		Jyothsna Chintala
    	on 21 Apr 2020
	
	
  	good
		Nicholas Bitler
    	on 26 Nov 2020
	
	
  	that was fun, took me a couple minutes
		Qingyin Zhang
    	on 29 Nov 2020
	
	
  	y = sum(b == a);
		Colin McNicholas
    	on 13 Dec 2021
	
	
  	Not too bad
		Hung Van Le
    	on 24 Feb 2022
	
	
  	yay
		Aman Yadav
    	on 31 Mar 2024
	
	
  	fggga
		MOHAMMAD SAMEER
    	on 22 May 2024
	
	
  	As difference in solution directly proportional to size of solution
Solution Comments
Show commentsProblem Recent Solvers13195
Suggested Problems
- 
         Find the numeric mean of the prime numbers in a matrix. 9052 Solvers 
- 
         Project Euler: Problem 10, Sum of Primes 1936 Solvers 
- 
         Set the array elements whose value is 13 to 0 1417 Solvers 
- 
         
         10768 Solvers 
- 
         
         1618 Solvers 
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!