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

 Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 5 Apr. 2019
Bearbeitet: Cris LaPierre am 5 Apr. 2019

1 Stimme

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 Kommentare

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
2CondPlot.png
Marisabel Gonzalez
Marisabel Gonzalez am 5 Apr. 2019
Bearbeitet: Marisabel Gonzalez am 5 Apr. 2019
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
2CondPlot_v2.png
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])
conditions.jpg
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
Marisabel Gonzalez am 6 Apr. 2019
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by