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

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.

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];

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

the result would be the same.

a = [1 2 3 4 5];

x = 2.3;

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

result = a == a(idx(1));

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?

http://www.mathworks.com/matlabcentral/answers/13149

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

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

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

please help me.

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

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.

Please help me!

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).

%Comments are welcome!

Matlab 8.4.0.150421 (R2014b)

Mahalo,

Roberto

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

B = [2 4 6];

C = setdiff(A,B)

C =

1 3 5

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

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?

your response is appreciated.

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,

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,

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.

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.