two conditions for plotting

6 views (last 30 days)
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')

Accepted Answer

Cris LaPierre
Cris LaPierre on 5 Apr 2019
Edited: Cris LaPierre on 5 Apr 2019
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 Comments
Cris LaPierre
Cris LaPierre on 6 Apr 2019
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])

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!