Plot a set of points in a standard simplex

33 次查看(过去 30 天)
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...

采纳的回答

Hassaan
Hassaan 2024-2-3
编辑:Hassaan 2024-2-3
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 个评论
Hassaan
Hassaan 2024-2-3
@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 2024-2-5
编辑:Dyuman Joshi 2024-2-6
@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].

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2024-2-3
编辑:Matt J 2024-2-3
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

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by