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 日間)
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

採用された回答

Star Strider
Star Strider 2015 年 8 月 13 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by