# How can I write a program to investigate whether a number belongs to matrix?

3 vues (au cours des 30 derniers jours)
Ayob le 17 Jan 2014
Commenté : Ayob le 17 Jan 2014
I have a Matrix named A and a number like B=11. I want to write a program which gives C=1 when B belongs to A and C=0 when B doesn't belong to A. I'm really interested in Matlab special features instead of long codes in my program.
e.g:
A=[1 2 3 5 21 2 4 0 11 3]
if B=11 then C=1
if B=9 then C=0
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Matt Kindig le 17 Jan 2014
I think ismember() should do the trick:
A=[1 2 3 5 21 2 4 0 11 3];
B = 11;
C = ismember(B, A); %returns 1
B = 9;
C = ismember(B, A); %returns 0
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Ayob le 17 Jan 2014
How easy!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by