Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Star Strider
Star Strider am 20 Nov. 2016
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.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by