Comparing the contents of two columns in MATLAB

I need to compare the contents of two tables, or rather two columns (one column per table), in MATLAB, to see each element of the first column, if the second column has an equal element.

Should I use a for loop or is there an existing MATLAB function that does this?

+4
source share
4 answers

If order is important, you are elemental comparison , after which you use all

%# create two arrays A = [1,2;1,3;2,5;3,3]; B = [2,2;1,3;1,5;3,3]; %# compare the second column of A and B, and check if the comparison is `true` for all elements all(A(:,2)==B(:,2)) ans = 1 

If order is unimportant and all elements are unique, use ismember

 all(ismember(A(:,1),B(:,1)) ans = 1 

If the order is unimportant and there are repetitions, use sort

 all(sort(A(:,1))==sort(B(:,2))) ans = 0 
+8
source

Did you know you can do this:

 >> a = [1:5]; >> b = [5:-1:1]; >> a == b ans = 0 0 1 0 0 

so that you can compare 2 columns in matlab using the == operator throughout the column. And you can use the result of this as an index specifier to get equal values. Like this:

 >> a(a == b) ans = 3 

This means that select all elements from a for which the value a == b is true.

For example, you can also select all elements from which more than 3:

 >> a(a > 3) ans = 4 5 

Using this knowledge, I would say that you can solve your problem.

+2
source

For arithmetic values, both solutions mentioned will work. For rows or arrays of cells, use strcmp / strcmpi .

In the help file:

TF = strcmp (C1, C2) compares each element of C1 with the same element in C2, where C1 and C2 are arrays of cells of equal row size. Input C1 or C2 can also be an array of characters with the correct number of lines. The function returns TF, a logical array that is the same size as C1 and C2, and contains logical 1 (true) for those elements C1 and C2 that are a match, and logical 0 (false) for those elements that are not do.

Example (also from the help file):

Example 2

Create 3 cells of row arrays:

 A = {'MATLAB','SIMULINK';'Toolboxes','The MathWorks'}; B = {'Handle Graphics','Real Time Workshop';'Toolboxes','The MathWorks'}; C = {'handle graphics','Signal Processing';' Toolboxes', 'The MATHWORKS'}; 

Compare cells of arrays A and B with case sensitivity:

 strcmp(A, B) ans = 0 0 1 1 

Compare cells of arrays B and C without case sensitivity. Note that the "Toolboxes" do not match due to leading space characters in C {2,1} that do not appear in B {2,1}:

 strcmpi(B, C) ans = 1 0 0 1 

To get a single return value, not an array of booleans, use the all function, as Jonas explained.

+1
source

You can use for loop (code below) to compare the contents of column 1 and column% 2 in the same table:

 clc d=[ 19 24 16 12 35 0 16 16 18 0 23 18 16 10 7 10 13 24 19 8 30 0 12 26 16 12 4 1 13 12 24 0 31 0 40 0 12 11 10 6 20 0 16 11 6 2 25 9 17 9 21 0 17 8 20 0 7 10 22 0 13 16 12 18 17 13 17 23 17 0 23 20 25 0 10 3 17 15 14 4 4 17 12 10 19 24 21 5 35 0 15 20 5 0 10 31 13 8 0 16 40 0 18 27 26 1 19 14 12 0 2 0 12 4 20 0 6 2 15 21 20 0 26 0 18 26 12 11 1 13 19 15 14 0 20 0 9 16 14 15 6 12 40 0 20 0 8 10 18 12 10 11 14 0 13 11 5 0 22 0 8 12 ]; x1=d(:,1); y1=d(:,2); for i=1:27 if x1(i)>y1(i); z(i)=x1(i); else z(i)=y1(i); end end z' 
-2
source

Source: https://habr.com/ru/post/1309254/


All Articles