If statement for equal rows from two different files

1 view (last 30 days)
Hello,
I have two files. Let's call them IDEAL and TEST.
Hypothetically, IDEAL contains the following data
4.1 5.3 0.02
0.4 1.0 1.11
5.8 0.4 0.85
9.0 0.3 0.34
and TEST contains
6.0 2.1 0.82
0.4 5.3 1.00
4.1 5.3 0.02
7.2 0.2 1.57
0.4 1.0 1.11
5.8 0.4 0.85
As you can tell, the files have the same number of columns but not the same number of rows, and some of the rows are identical.
I want to write an if statement in which says that
  • if TEST has rows that IDEAL does not have, plot (X,Y) as red points
  • if TEST has rows that IDEAL does have, plot (X,Y) as blue points
  • if TEST does not include rows that are in IDEAL, plot (X,Y) as green points
X and Y use several values from my files, i.e. they include several columns. I'm more interested in how to write the stament for the rows. The rows do not need to be in the same position, I just want MATLAB to know that if both files have exactly the same row, independent of position, to plot my (X,Y) as blue and if not, as red.
PS: not always all of the rows in IDEAL are included in TEST (e.g. last row in IDEAL).

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 4 Apr 2019
Try this:
A = [4.1 5.3 0.02;0.4 1.0 1.11;5.8 0.4 0.85]
B = [6.0 2.1 0.82;0.4 5.3 1.00;4.1 5.3 0.02;7.2 0.2 1.57;0.4 1.0 1.11;5.8 0.4 0.85];
C = setdiff(B,A,'rows','stable') %Rows only in B
D = setdiff(B,C,'rows','stable') %Rows in A and B
  3 Comments
Alex Mcaulley
Alex Mcaulley on 4 Apr 2019
Edited: Alex Mcaulley on 4 Apr 2019
A = [4.1 5.3 0.02;0.4 1.0 1.11;5.8 0.4 0.85;9.0 0.3 0.34]
B = [6.0 2.1 0.82;0.4 5.3 1.00;4.1 5.3 0.02;7.2 0.2 1.57;0.4 1.0 1.11;5.8 0.4 0.85];
C = setdiff(B,A,'rows','stable') %Rows only in B
D = setdiff(B,C,'rows','stable') %Rows in A and B
E = setdiff(A,[C;D],'rows','stable') %Rows only in A

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!