Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing content of two columns in MATLAB

Tags:

matlab

I need to compare the content of two tables, more exactly two columns (one column per table), in MATLAB to see for each element of the first column, if there is an equal element in the second column.

Should I use a for loop or is there an existing MATLAB function that does this?

like image 209
aneuryzm Avatar asked May 09 '10 11:05

aneuryzm


1 Answers

If the order is important, you do element-wise comparison, after which you use all

%# create two arrays
A = [1,2;1,3;2,5;3,3];
B = [2,2;1,3;1,5;3,3];

%# compare the second column of A and B, and check if the comparison is `true` for all elements
all(A(:,2)==B(:,2))
ans = 
    1

If the order is unimportant and all elements are unique, use ismember

all(ismember(A(:,1),B(:,1))
ans = 
    1

If the order is unimportant, and there are repetitions, use sort

all(sort(A(:,1))==sort(B(:,2)))
ans = 
    0
like image 178
Jonas Avatar answered Sep 28 '22 08:09

Jonas