Plot a set of points in a standard simplex

31 views (last 30 days)
How do I plot a 2D standard (probability) simplex (just a plot of an equilater triangle) and plot a set of points (probability vectors of size 3) inside the triangle? And how to do the same thing in 3D? I have looked at Matlab documentantion, but could not find a direct way to do it...

Accepted Answer

Hassaan
Hassaan on 3 Feb 2024
Edited: Hassaan on 3 Feb 2024
Plotting in 2D
  1. Plot the Equilateral Triangle (Simplex): You can plot an equilateral triangle by defining its vertices and using the plot function.
  2. Transform Probability Vectors: probability vectors (for 2D simplex, vectors of size 3 summing to 1) do not directly correspond to Cartesian coordinates, you'll need to transform them into the 2D space of the simplex.
  3. Plot Points: Use the transformed coordinates to plot points within the simplex.
% Define vertices of the equilateral triangle
vertices = [0, 0; 1, 0; 0.5, sqrt(3)/2];
vertices = [vertices; vertices(1,:)]; % Close the triangle
% Plot the simplex (equilateral triangle)
figure;
plot(vertices(:,1), vertices(:,2), '-');
hold on;
axis equal;
grid on;
% Example set of points (probability vectors)
P = [0.1, 0.6, 0.3; 0.3, 0.3, 0.4; 0.25, 0.25, 0.5];
% Transform and plot points
for i = 1:size(P,1)
% Transform points to Cartesian coordinates
x = P(i,1) + P(i,2)/2;
y = P(i,2)*sqrt(3)/2;
plot(x, y, 'ro');
end
title('2D Simplex with Points');
xlabel('X');
ylabel('Y');
Extending to 3D
% Define vertices of the tetrahedron
vertices = [0, 0, 0; 1, 0, 0; 0.5, sqrt(3)/2, 0; 0.5, sqrt(3)/6, sqrt(2/3)];
faces = [1, 2, 3; 1, 2, 4; 2, 3, 4; 1, 3, 4];
% Plot the simplex (tetrahedron)
figure;
hold on;
trisurf(faces, vertices(:,1), vertices(:,2), vertices(:,3), 'FaceColor', 'none', 'EdgeColor', 'b');
axis equal;
grid on;
% Example set of points (probability vectors for 3D simplex)
P = [0.1, 0.2, 0.3, 0.4; 0.25, 0.25, 0.25, 0.25];
% Transform and plot points
for i = 1:size(P,1)
% Transform points to Cartesian coordinates (specific to tetrahedron layout)
x = P(i,1) + P(i,2)/2 + P(i,4)/3;
y = P(i,2)*sqrt(3)/2 + P(i,4)*sqrt(3)/6;
z = P(i,4)*sqrt(2/3);
plot3(x, y, z, 'ro');
end
title('3D Simplex with Points');
xlabel('X');
ylabel('Y');
zlabel('Z');
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  3 Comments
Hassaan
Hassaan on 3 Feb 2024
@valentino dardanoni You are welcome.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
Dyuman Joshi
Dyuman Joshi on 5 Feb 2024
Edited: Dyuman Joshi on 6 Feb 2024
@Hassaan, What is the transformation you do on the probability vectors? What is it called? What is the mathematical basis of it?
Your solution hard-codes values, which is not a good approach. It only works for some specific cases, and fails for countless other triangles, e.g - [5, 5; 6, 5; 5.5, 5+sqrt(3)/2].

Sign in to comment.

More Answers (1)

Matt J
Matt J on 3 Feb 2024
Edited: Matt J on 3 Feb 2024
For 2D:
p=nsidedpoly(3);
c=rand(10,3); c=c./sum(c,2);
v=num2cell(c*p.Vertices,1); %"probability vectors"
plot(p); axis equal; hold on
scatter(v{:},'filled'); hold off
For 3D, you would do similarly, but you would need scatter3() and you would need to download plotregion().
figure;
plotregion(-[1,1,1],-1,[0,0,0]); view(3);hold on
v=num2cell(c,1);
scatter3(v{:},'filled'); hold off

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!