If statement for equal rows from two different files
조회 수: 1 (최근 30일)
이전 댓글 표시
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).
댓글 수: 0
채택된 답변
Alex Mcaulley
2019년 4월 4일
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
Alex Mcaulley
2019년 4월 4일
편집: Alex Mcaulley
2019년 4월 4일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!