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
0 个评论
采纳的回答
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 Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!