two conditions for plotting
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi,
I have the following code but it is not doing what I want it to do yet. I was wondering if you could help me fix it.
Instead of only plotting the data points for C it is doing for all the outputs of C, D and E.
What I am trying to do for C, I am also truying for D and E.
A = [5 5.3 0.02; 10 1.0 1.11; 10 0.4 0.85; 3 0.3 0.34];
B = [3 2.1 0.82; 10 5.3 1.00; 5 5.3 0.02; 5 0.2 1.57; 10 1.0 1.11; 10 0.4 0.85];
%% First set of conditions
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
%% Second set of conditions
% for C
already_plotted = false(size(C));
L= C(:,1) == 5;
plot(A(L),B(L),'rp')
already_plotted(L)=true;
L= C(:,1) == 10 & ~already_plotted;
hold on
plot(A(L),B(L),'ro')
already_plotted(L)=true;
L= C(:,1) == 3 & ~already_plotted;
hold on
plot(A(L),B(L),'rs')
채택된 답변
Cris LaPierre
2019년 4월 5일
편집: Cris LaPierre
2019년 4월 5일
1 개 추천
I'm not exactly sure what you are trying to do, but you are being very careless with your indeces. For starters:
- A is a 4x3 matrix
- B is a 6x3 matrix
- C is a 3x3 matrix
- already_plotted is a 3x3 matrix
- L is a 3x1 matrix
What values of A and B did you want to plot?
- A(L) is using a 3x1 matrix to index values from a 4x3 matrix.
- B(L) is using a 3x1 matrix to index values from a 6x3 matrix.
- Also, L is a variable generated based on C. How is that suppose to relate back to A and B?
Similarly, what is the intended outcome of this code?
- already_plotted(L)=true is assigning a 1x1 value to a 4x3 matrix using a 3x1 index.
- L= C(:,1) == 10 & ~already_plotted; is using an & to combine a 3x1 condition and a 3x3 condition
The point is you have to be much more explicit about what values you want.
댓글 수: 7
Not sure if this is what you are trying to do, but see if this helps you out.
A = [5 5.3 0.02; 10 1.0 1.11; 10 0.4 0.85; 3 0.3 0.34];
B = [3 2.1 0.82; 10 5.3 1.00; 5 5.3 0.02; 5 0.2 1.57; 10 1.0 1.11; 10 0.4 0.85];
%% First set of conditions
[C,ic] = setdiff(B,A,'rows','stable'); %Rows only in B
[D, id] = setdiff(B,C,'rows','stable'); %Rows in A and B
[E,ie] = setdiff(A,[C;D],'rows','stable'); %Rows only in A
%% Second set of conditions
% for C
already_plotted = false(size(ic));
L= C(:,1) == 5;
plot(A(ic(L),:),B(ic(L),:),'rp')
hold on
already_plotted(L)=true;
L= C(:,1) == 10 & ~already_plotted;
plot(A(ic(L),:),B(ic(L),:),'ro')
already_plotted(L)=true;
L= C(:,1) == 3 & ~already_plotted;
plot(A(ic(L),:),B(ic(L),:),'rs')
hold off

Marisabel Gonzalez
2019년 4월 5일
편집: Marisabel Gonzalez
2019년 4월 5일
Thank you for your answer Cris LaPierre. But that is not what I am trying to do. A and B have intentionally different sizes. Originally, I would be comparing two long files of different sizes and would like to plot as different symbols/colours:
- their rows in common (D)
- rows that only A has (E)
- rows that only B has (C)
Both files will have for example 10, 5 and 3 in their first column and I would also like to plot those with different symbols/colours in the same plot for the three bullet points above, i.e. for C,D and E.
So for C, there should only be 3 points if you have a look at the output (in my original code).
Only using C as an example. Once I know which rows are only in B, I would like to plot only the values of C with 10, 5 and 3 as different colours. I see the confusion now. Let's say I only want to plot the first column of C against the third column.
Then, I would like to apply the same process for D and E (plotting column1 vs column3).
Sorry for the confusion. Believe me, it was also confusing to me in how to write the question in the first place. Hope it is more clear now.
Not sure I fully get it still, but here's a new approach.
- Symbol stays the same within a group (only in A, only in B, in A&B)
- Color changes for each point
A = [5 5.3 0.02; 10 1.0 1.11; 10 0.4 0.85; 3 0.3 0.34]
B = [3 2.1 0.82; 10 5.3 1.00; 5 5.3 0.02; 5 0.2 1.57; 10 1.0 1.11; 10 0.4 0.85]
%% First set of conditions
[C,inB] = setdiff(B,A,'rows','stable') %Rows only in B
[D,inBA] = setdiff(B,C,'rows','stable') %Rows in B also in A
[E,inA] = setdiff(A,B,'rows','stable') %Rows only in A
[F,inAB] = setdiff(A,E,'rows','stable') %Rows in A also in B (redundant)
%% Second set of conditions
% in B only
cmap = jet(length([inB; inBA; inA]));
scatter(C(:,1),C(:,3),100,cmap(1:length(inB),:),'filled','s','MarkerEdgeColor','k')
hold on
% in A only (E)
scatter(E(:,1),E(:,3),100,cmap(length(inB)+1:length([inB;inA]),:),'filled','o','MarkerEdgeColor','k')
% in A & B (D)
scatter(D(:,1),D(:,3),100,cmap(length([inB;inA])+1:end,:),'filled','p','MarkerEdgeColor','k')
hold off

The legend isn't quite accurate, but that's more for you.
This part is missing...
% for C
already_plotted = false(size(C));
L= C(:,1) == 5;
plot(A(L),B(L),'rp') % wrong should plot C(:,1) and C(:,3)
already_plotted(L)=true;
hold on
L= C(:,1) == 10 & ~already_plotted;
plot(A(L),B(L),'ro') % wrong should plot C(:,1) and C(:,3)
already_plotted(L)=true;
%L= ~already_plotted;
L= C(:,1) == 3 & ~already_plotted;
plot(A(L),B(L),'rs') % wrong should plot C(:,1) and C(:,3)
But I don't know how to apply the conditions and select the row at the same time. You cannot actually write the following, can you?
plot(C(:,1)(L),C(:,3)(L))
Thank you for still trying. I deeply appreciate it =)
Hi Cris LaPierre, I managed to get it to work how I wanted it to. The symbols just look funy. Not sure if it is because of the MATLAB version that I am using. Normally I use my personal laptop.
Find below the code that worked and the outpt that I was expecting despite the weird symbols
clear all; close all; clc
A = [5 5.3 0.02; 10 1.0 1.11; 10 0.4 0.85; 3 0.3 0.34];
B = [3 2.1 0.82; 10 5.3 1.00; 5 5.3 0.02; 5 0.2 1.57; 10 1.0 1.11; 10 0.4 0.85];
%% First set of conditions
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
%% Second set of conditions
% for C
already_plotted = false(size(C));
L= C(:,1) == 5;
plot(C(:,1),C(:,3),'rp')
already_plotted(L)=true;
hold on
L= C(:,1) == 10 & ~already_plotted;
plot(C(:,1),C(:,3),'ro')
already_plotted(L)=true;
%L= ~already_plotted;
L= C(:,1) == 3 & ~already_plotted;
plot(C(:,1),C(:,3),'rs')
% for D
already_plotted = false(size(D));
L= D(:,1) == 5;
plot(D(:,1),D(:,3),'bp')
already_plotted(L)=true;
hold on
L= D(:,1) == 10 & ~already_plotted;
plot(D(:,1),D(:,3),'bo')
already_plotted(L)=true;
%L= ~already_plotted;
L= D(:,1) == 3 & ~already_plotted;
plot(D(:,1),D(:,3),'bs')
% for E
already_plotted = false(size(E));
L= E(:,1) == 5;
plot(E(:,1),E(:,3),'gp')
already_plotted(L)=true;
hold on
L= E(:,1) == 10 & ~already_plotted;
plot(E(:,1),E(:,3),'go')
already_plotted(L)=true;
%L= ~already_plotted;
L= E(:,1) == 3 & ~already_plotted;
plot(E(:,1),E(:,3),'gs')
axis([2 11 -0.5 2])

which follows my outputs for C, D and E
C =
3.0000 2.1000 0.8200
10.0000 5.3000 1.0000
5.0000 0.2000 1.5700
D =
5.0000 5.3000 0.0200
10.0000 1.0000 1.1100
10.0000 0.4000 0.8500
E =
3.0000 0.3000 0.3400
The part you say is missing I didn't find necessary. At the least, I didn't see what adding it accomplished. I found that setdiff accomplished everything I needed.
Rather than compare code, compare my plot to yours. What is missing or wrong?
When I run your code, I get the same weird symbols. This is because you now plot every point with every symbol. You're not using L anymore. Modifying your code, what about this?
A = [5 5.3 0.02; 10 1.0 1.11; 10 0.4 0.85; 3 0.3 0.34];
B = [3 2.1 0.82; 10 5.3 1.00; 5 5.3 0.02; 5 0.2 1.57; 10 1.0 1.11; 10 0.4 0.85];
%% First set of conditions
C = setdiff(B,A,'rows','stable') %Rows only in B
D = setdiff(B,C,'rows','stable') %Rows in A and B
E = setdiff(A,B,'rows','stable') %Rows only in A
%% Second set of conditions
% for C
L= C(:,1) == 5;
plot(C(L,1),C(L,3),'rp')
hold on
L= C(:,1) == 10;
plot(C(L,1),C(L,3),'ro')
L= C(:,1) == 3;
plot(C(L,1),C(L,3),'rs')
% for D
L= D(:,1) == 5;
plot(D(L,1),D(L,3),'bp')
L= D(:,1) == 10;
plot(D(L,1),D(L,3),'bo')
L= D(:,1) == 3;
plot(D(L,1),D(L,3),'bs')
% for E
L= E(:,1) == 5;
plot(E(L,1),E(L,3),'gp')
L= E(:,1) == 10;
plot(E(L,1),E(L,3),'go')
L= E(:,1) == 3;
plot(E(L,1),E(L,3),'gs')
hold off
axis([2 11 -0.5 2])
Marisabel Gonzalez
2019년 4월 6일
Thanks!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
