MATLAB: Compare matrices with different dimension

ismember

Hi,
A = [100123 1 1 50;
100123 1 2 53;
100123 1 3 55;
100456 2 1 78;
100456 2 2 75;
100789 1 1 80]
B = [100123 1 56;
100456 2 76]
Comparing the 1st and 2nd columns of A and B, I've to compute the difference between each value in the 4th comlumn of A and the values in the 3th column of B.
In the case to compute (A – B), the expected result is a Matrix as
C = [100123 1 1 -6;
100123 1 2 -3;
100123 1 3 -1;
100456 2 1 2;
100456 2 2 -1]
Tnx for any suggestion

Best Answer

You could try this:
>> X = bsxfun(@eq,A(:,1).',B(:,1));
>> Y = bsxfun(@minus,A(:,4).',B(:,3));
>> Z = [A(any(X,1),1:3),Y(X)]
Z =
100123 1 1 -6
100123 1 2 -3
100123 1 3 -1
100456 2 1 2
100456 2 2 -1
It returns only the rows of A for which the first element matches any first row element in B, and in the last column gives the difference of corresponding end elements of A and B.