Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab, How do I compare each element of a row matrix with each element of another row matrix?

Tags:

matlab

I have two matrices in Matlab:

q = [3 4 5];  
w = [5 6 7];

I want to compare every element of q with w (i.e. 3 compared with 5, 6, and 7). If it matches any element in w (like how 5 is in both q and w) then both q and w share 5 as a common key.

How can I compute all the common keys for q and w?

like image 560
gurwinder Avatar asked Nov 17 '09 12:11

gurwinder


1 Answers

Try

>> x = intersect(q,w)

x = 

    5

This function treats the input vectors as sets and returns the set intersection. I think this is what you wanted to know. Is there a match yes/no? if x is empty (numel(x)==0) there was no match.

like image 125
sellibitze Avatar answered Sep 19 '22 23:09

sellibitze