MATLAB: Two conditions for plotting

graphicsifif statementloopMATLABplot

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')

Best Answer

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.