how can i compare the elements of two matrices A(m*5) and B(n*5) in each row and see if they match?

1 view (last 30 days)
i have two matrices, lets say
A=[
1 0 0 0 5
1 1 1 0 8
1 4 3 2 10
2 0 1 0 8
2 1 2 1 7] B =
1 0 0 0 10
1 3 5 7 0
1 5 7 8 2
1 6 8 9 3
1 1 1 0 3
1 5 6 8 7
1 4 3 2 10
2 0 1 0 3
2 5 6 8 7
2 6 8 9 3
2 1 2 1 4
2 3 5 7 0
all the rows of matrix A but are somewhere in the matrix B but the last elements in rows are different(last column of A&B) , the only constraint about matrices A and B is that the elements of first columns of matrices A&B are either 1 or 2. i want to compare elements of first 4 elements at each row from matrix B with matrix A, and if the first 4 elements match, take the difference of last element.
can somebody please help me.
thank you

Accepted Answer

Star Strider
Star Strider on 13 Aug 2015
This works:
A = [1 0 0 0 5
1 1 1 0 8
1 4 3 2 10
2 0 1 0 8
2 1 2 1 7];
B = [1 0 0 0 10
1 3 5 7 0
1 5 7 8 2
1 6 8 9 3
1 1 1 0 3
1 5 6 8 7
1 4 3 2 10
2 0 1 0 3
2 5 6 8 7
2 6 8 9 3
2 1 2 1 4
2 3 5 7 0];
A4 = A(:,1:4); % First 4 Columns Of A
B4 = B(:,1:4); % First 4 Columns Of B
[C4, L] = ismember(A4, B4, 'rows'); % Find Rows That Match (‘L’)
D5 = A(:,5) - B(L,5); % Take Difference In Last Column

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!