jagan
jagan

Reputation: 11

Compare arrays in matlab by elements

I want to compare multiple arrays in MATLAB element-by-element; and if they equal at any position, increment the count. (i.e whole array need not be equal, whenever the comparison finds the elements equal for the FIRST time that is enough. Ex A=[1 2 3], B=[4 2 5], here at position 2 its equal.)

I tried isequal(A,B): that compares all the elements and returns Count 1 or 0; but isequal(A[i], B[i]); does not work to compare numbers as element by element.

Can you suggest any solution please..

Upvotes: 0

Views: 2014

Answers (4)

user3329648
user3329648

Reputation: 89

If you have 3 arrays that are all the same size (A, B, and C), and you just wanted to know where your equalities are, you could use:

R(:,:,1) = bsxfun(@eq,A,B);
R(:,:,2) = bsxfun(@eq,C,B);
R(:,:,3) = bsxfun(@eq,A,C);
IDX = find(R);
[Rows,Columns,~] = ind2sub(size(R),IDX);

and that will give you two vectors containing the row and column locations of all of your equalities. I tried this using the arrays:

A = [4 6 2 4
     3 5 4 4];
B = [6 2 6 3 
     2 1 4 4];
C = [4 4 4 2
     4 5 6 1];

and got

Rows = [2 2 1 2]
Columns = [3 4 1 2]

which do correlate to the locations of the various equalities. If you want to find the first location, just run:

CL = min(Columns);
RW = min(Rows(Columns == CL));

and that will give you the row-column index of the first equality. In the above example, it gives RW = 1, CL = 1

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112679

From your question I'm not sure what you want. I assume that, given a set of row vectors with the same number of columns, you want to know at how many columns all those vectors have the same value (but the value can be different for each column).

I also assume your data is in the form of rows of a matrix, for example

 data = [ 1 2 9 3 5
          4 5 9 3 5 
          7 8 9 2 5 ];

Then:

result = sum(all(diff(data)==0));

In this case the result is 2, because the equality condition is satisfied at two columns (third and fifth).


If you want to find the index of the first column where at least two rows have equal values:

 result = find(any(diff(sort(data))==0),1);

For example,

data = [ 1 8 9 3 5
         4 5 9 3 5 
         7 8 9 2 5 ];

would give 2, because the second column is the first that has equal values.

Upvotes: 0

LordViaderko
LordViaderko

Reputation: 131

Try:

if(sum(A == B))
    % increment your counter
end;

EDIT:

Why it works: (A == B) returns a new array, with ones on pleces at wchich A and B have the same values, and zeros otherwise. "Sum" sums over this new array, and if this sum is non-zero (i.e. A and B have at least one common value at the same place), "if" is triggered. Otherwise sum returns zero, which is treated by if statement as "false".

Upvotes: 1

Intrepid Traveller
Intrepid Traveller

Reputation: 95

use a for loop using an index counter that goes through the index of an array one by one and compare the elements of the arrays one by one.

Upvotes: 0

Related Questions