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(최근 30일)
Joseph 13 Aug 2015
Commented: Star Strider 13 Aug 2015
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.
thank you

로그인 to comment.

채택된 답변

Star Strider 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

댓글 수: 2

Joseph 13 Aug 2015
thank you. helped alot.
Star Strider 13 Aug 2015
My pleasure.

로그인 to comment.