# Find and match multiple values in a matrix with MATLAB's ismember function

I was preparing some results of an image segmentation algorithm for publishing and needed a way to find all pixels that match a variable set of multiple numbers (e.g. find all elements in the array `a` that has value 1 or 2). Normally, you can use `find` with a boolean or, as in:

`find(a == 1 | a == 2)`

This lets you find all the subscripts of `a` that is equal to 1 or 2. This is fine but gets cumbersome if you had to match a dozen possible numbers or if the numbers you want to match come as a vector or list (e.g. find all elements in `a` that match `[1 2 4 5]`). Then you would have to use some kind of `for` loop with `find`. I just found out that there is actually an easier way using MATLAB's `ismember` function.

Make an array to test with:

```>> a = [1 2; 4 5; 7 8]

a =

1     2
4     5
7     8```

Now, let's look at how the condition parameter in `find` actually works:

```>> (a == 1 | a == 2)

ans =

1     1
0     0
0     0```

The parameter that `find` takes is simply a logical array where elements in `a` that match either 1 or 2 are 1 and all other elements are 0.

Let's see what `ismember` does:

```>> ismember(a,[1 2])

ans =

1     1
0     0
0     0```

This produces the same output, but you can specify the multiple values you want to match using an array. So the following works too:

```>> ismember(a,[1 2 4 5])

ans =

1     1
1     1
0     0```

Thus, you can do this with `find`:

```>> find(ismember(a,[1 2 4 5]))

ans =

1
2
4
5```

This lists the subscripts for all elements in `a` that are equal to 1, 2, 4 or 5.

## Discussion

, 2011/07/20 15:04
, 2012/01/25 05:08
Thank You VERY MUCH!!! explained excellent !
, 2012/05/06 06:04
Hi,

This is pretty interesting one. But I would like to know the other elements around instead of similar elements.

say a = [ 1 2 3] and b = [2 3].
I need the solution such that "1" is not present in b.

Thank you,
Krishna.
, 2012/05/21 21:50
Try using "~ismember" instead of ismember. That gives the opposite of ismember.
, 2012/08/21 12:57
Interesting.
But it seems that the order is not correct.

say a = [5 4 3 2 1]; b = [1 2];
and run find(ismember(a,b))
the result is [4 5].

But I expect result [5 4];
, 2012/08/29 21:50
I don't think you can do anything about that. Even if you did:

find(a == 1 | a == 2)

the result would be the same.
, 2013/02/12 08:49
Hi, let's say we have a=[1 2 3 4 5] and I have a number x=2.3 How do I find the nearest element to x in a (i.e. 2 in this case) and also I would like to have a 1 there and 0 elsewhere. I hope I made my question somewhat clear?
, 2013/02/12 14:07
That should be easy enough with something like this:

a = [1 2 3 4 5];
x = 2.3;

[m,idx] = min(abs(a .- x));

result = a == a(idx(1));
, 2013/05/02 17:05
Hi Peter,

This is what I'm working on:

for i = 1:25
j = randi(365,1,25,3);
end
sort(j)

This gives me 3 sets of 25 random numbers then sorts them in numerical order, I need to know how many of those numbers are the same (and possibly which numbers they are). Any suggestions?
, 2013/05/13 14:41

, 2013/08/24 03:58
i want to match two images pixel one by one and want to overlap these images..
, 2013/10/16 02:00
Dear All,

I have a matrix, say M x 9 elements, and trying to look for indices of the medians in every row. How would I do that with vectorization? if any one can help?

Regards,

Munir
, 2013/11/02 20:34
hi,

i want to check the conditions for each matrix elements. say i have x=[0.1 0.2 0.3; 0.2 0.3 0.4 ;0.5 0.6 0.7].i want to check each element which is less than 0.25,0.5 0.75,1 and store in 4 different matrix with same size.we cant get equal matrix.so i want to put 0 in all the other locations to make it as equal matrix.can you give any suggestions
, 2014/05/21 07:21
I want to find the index of zeros which is in consecutive order and it should be more or equal to 5 time.
i wrote this code but it does't work.
a=[0,0,0,0,0,0,0,0,9,8,5,6,0,0,0,0,0,0,3,4,6,8,0,0,9,8,4,0,0,7,8,9,5,0,0,0,0,0,8,9,0,5,8,7,0,0,0,0,0];
[x,y]=size(a);

for i=0:y
i+1;
k=1;
l=0;
n=i;
count=0;

while (a==0)
count+1;
break;
n+1;
end
if(count>=5)
v([]);
for l=k:l<n
v(m)=l+1;
m+1;
end
end
count=1;
i=n;
end
for i=o:i<m
i+1;
fprintf('index of continous zero more than 5 or equal=%d',v(i));
end

, 2014/07/25 11:17
This is very useful function.

I wonder if i can use that function (or there are some) in Matlab that

I can check if there is nearest value vector in dataset with vector A.

This is kind of finding nearest neighbor vector. Each vector has 30 values.

Vector A, VECTOR W, (to see if any vector on left similar-entries not eact

Vector B, same).

Vector D,

Vector E,

Vector F,

Vector H,

Kindly,

Wali
, 2014/09/06 03:19
if find(ismember(DSTSet(:,2),[diain])&& find(ismember(DSTSet(:,3),[diain])))
, 2014/11/18 12:57
Hi,

I have an variable of size n*3. I want to find index which matches all the three elements.

For ex

l = 3 4 1

4 4 2

5 8 2

4 8 9

2 2 0

6 7 9

2 5 8

1 3 5

and I want to find index matching [2 2 0],

In result I want '5' in this case.

, 2015/07/11 12:49
hi,I have a 41*1499 matrix,I need to delete some columns with specific figures ,also I need a code which delete the same columns with more than 80 percent similarity.
, 2016/02/05 16:37
Aloha Peter,

Thank you so much for helping us in this Matlab endeavor.

Could you look at this, seems something is wrong here...

%Example: 3rd value is different below, everything else is the same.

a1 = [1.2000 2.0003 4.8541 5.0100 7.0000 8.8700]
a2 = [1.2000 2.0003 4.8542 5.0100 7.0000 8.8700]
D1=ismember(a1, [2:7])
D11=ismember(a2, [2:7])
D2=ismember(a1, [2:0.001:7])
D22=ismember(a2, [2:0.001:7])
D3=ismember(a1, [2:0.0001:7])
D33=ismember(a2, [2:0.0001:7])
a1 =
1.2000 2.0003 4.8541 5.0100 7.0000 8.8700
a2 =
1.2000 2.0003 4.8542 5.0100 7.0000 8.8700
D1 =
0 0 0 0 1 0
D11 =
0 0 0 0 1 0
D2 =
0 0 0 1 1 0
D22 =
0 0 0 1 1 0
D3 =
0 1 1 1 1 0
D33 =
0 1 0 1 1 0

%Results are the same for D1 and D11, D2 and D22, but different for D3 and D33 as the 3rd value is not identified in D33 (here 3rd value = 4.8542).

Matlab 8.4.0.150421 (R2014b)

Mahalo,
Roberto
, 2016/08/24 06:15

A = [3 6 2 1 5 1 1];

B = [2 4 6];

C = setdiff(A,B)

C =
1 3 5
, 2016/08/25 19:36
Hi,

I have the matrix A:

A=[1 5;

2 4;

4 9;

6 3;]

All the elements of the 1st column are integer and arranged ascendingly but there is a jump somewhere (from row 2 to 4 missing 3 and from 4 to 6 missing 5).

I want to add zero rows wherever there is jump in the 1st column as:

A=[1 5;

2 4;

0 0;

4 9;

0 0;

6 3;]

Thanks
, 2016/11/12 14:40
it was great.

but what if we want to find the position of the common array between the matrices of 2 images?

i.e. a 10x10 matrix of a image A is located where in image B?

, 2017/05/29 11:50
Please at my matlab program, here my problem is i' want to get the greater entrie of matrix A, get at once the position of this greater entrie of A

function [m,a,b]=abela(A)
A=[1 2 3;1 5 7; 9 8 11];
for i=1:3
for j=1:3
d=A(i,j);
m=sort(max(d));
u=='m';
[i,j]=ind2sub(size(A(i,j)),find(u==m));

end
end
m,i,j,
, 2017/05/29 11:51
Please look at my matlab program, here my problem is i' want to get the greater entrie of matrix A, get at once the position of this greater entrie of A

function [m,a,b]=abela(A)
A=[1 2 3;1 5 7; 9 8 11];
for i=1:3
for j=1:3
d=A(i,j);
m=sort(max(d));
u=='m';
[i,j]=ind2sub(size(A(i,j)),find(u==m));

end
end
m,i,j,
, 2017/09/02 09:46
hello,

I have a matrix of 178*13(dataset), from which I want to delete some data. I want to remove 1% of random data from my original data set. How can I do that?

Any response is appreciated.
, 2018/03/01 01:16
Hello,

I would like to know if there is any way we can reposition the elements in the matrix according to elements in another array.

eg. A=[1 2 3 5 6 8 9 12 4 5 6 8] is to be rearranged according to another array B=[5 6 1 7 8 9 10 12 3 11 1 2 4], where B contains respective positions i.e, element 1 is to present at postion 5.

Thank you.
, 2018/04/17 00:18
Thank you, your post helped me find a solution to my problem.