Compare each row of array, then return the number of rows that are equal

1 次查看(过去 30 天)
Hello, I was trying to compare each row, one by one, and then return the number of rows that are equal.
For example, for the arrays A and B:
A = [1;1;2;3];
B = [2;1;1;3];
The rows that are equal to each other = 2...that is, rows 2 and 4 are equal. I tried the command:
all(A()==B())
but got the ans = 0.
Any help is appreciated.

采纳的回答

Star Strider
Star Strider 2016-11-20
Try this:
A = [1;1;2;3];
B = [2;1;1;3];
C = [A, B];
Equal_Rows = find(diff(C,[],2) == 0)
Equal_Rows =
2
4
NOTE This only reliably works with integers. For floating point numbers, especially those that are the result of calculations, you would need to incorporate a tolerance, probably involving a ‘greater-than-or-equal to’ and a ‘less-than-or-equal-to’ condition. See Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? for an explanation.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by