Two similar elements in legend, when they should differ

1 vue (au cours des 30 derniers jours)
Jonne Klockars
Jonne Klockars le 14 Oct 2022
I wrote a function for XOR-problem (below). However, the legend behaviour is weird. First I thought I'd solve it by adding an empty string to the legend (two green discs are plotted), but the problem of legend showing both XOR values as green discs persists. I don't know why the second value (XOR=0) is not a red dicsc in the legend. If someone could point out how to correct the color, I'd appreciate that a lot.
% XOR implemented by projecting the data into third dimension
% returns zero, draws the decision plane in 3D.
function a = XOR3D(data)
z = zeros(3,size(data,2)); % z-axis for the projection
x = data(1,:); % x-coordinates of the data
y = data(2,:); % y-coordinates of the data
for i = 1:size(data,2)
% check if the coordinates are the same or not
if x(i) ~= y(i)
z(:,i) = [0,0,1]'; % XOR = 1
else
z(:,i) = [0,0,2]'; % XOR = 0
end
end
figure;
[X,Y] = meshgrid(2:0.1:3);
Z = repmat(1.5,size(X,1));
mesh(X,Y,Z);
hold on;
% THIS IS GREEN IN THE LEGEND: OK
plot3(2,2,z(z<2),'o','MarkerFaceColor','g','MarkerSize',10);
hold on;
% THIS SHOULD BE RED IN THE LEGEND, NOT GREEN LIKE IT IS NOW
plot3(2,2,z(z>1),'o','MarkerFaceColor','r','MarkerSize',10);
hold on;
plot3(x,y,zeros(4,1),'o','MarkerFaceColor','k','MarkerSize',10);
hold on;
quiver3(2,2,0,0,0,2.21,'r');
hold on;
quiver3(3,3,0,-1.11,-1.11,2.21,'r');
hold on;
quiver3(2,3,0,0,-1.11,1.11,'g');
hold on;
quiver3(3,2,0,-1.11,0,1.11,'g');
title('XOR by projection into the 3rd dimension.');
legend('decision plane z=1.5','XOR=1','','XOR=0');
xlabel('X'); ylabel('Y');
a = 0;
end
Here is an image displaying both XOR values as green discs in the legend:

Réponse acceptée

Walter Roberson
Walter Roberson le 14 Oct 2022
plot3(2,2,z(z<2),'o','MarkerFaceColor','g','MarkerSize',10);
That does not mean to draw the selected z values with constant x and constant y, all as a single line. Instead it means to draw numel(z(z<2)) separate lines.
Consider
z2 = z(z<2);
z2xy = 2 * ones(size(z2));
plot3(z2xy, z2xy, z2, 'o', 'MarkerFaceColor', 'g', 'MarkerSize', 10);
  1 commentaire
Jonne Klockars
Jonne Klockars le 14 Oct 2022
Modifié(e) : Jonne Klockars le 14 Oct 2022
Yep, now it works. Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by