How to check a number is in a array or not?

2.620 visualizaciones (últimos 30 días)
Arnab Pal
Arnab Pal el 29 de Ag. de 2017
Comentada: Matthew Coile el 17 de Feb. de 2023
a=[8 9 6 5 3], I want to know 5 is there or not.

Respuesta aceptada

Guillaume
Guillaume el 29 de Ag. de 2017
ismember(5, a)
  2 comentarios
Putri Basenda Tarigan
Putri Basenda Tarigan el 16 de Nov. de 2020
Excuse me Sir,
For the same problem, if we don't know that the number is 5, how to code to get that the number is 5?
Matthew Coile
Matthew Coile el 17 de Feb. de 2023
Putri, you can just as easily use a variable. That is, ismember(x,a) would work as well, where the value of x is assigned earlier in the script.

Iniciar sesión para comentar.

Más respuestas (1)

José-Luis
José-Luis el 29 de Ag. de 2017
Editada: José-Luis el 29 de Ag. de 2017
sum(a==5) > 0
any(a==5)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by