Create and plot random points with connected lines

1 回表示 (過去 30 日間)
Ismaeel
Ismaeel 2017 年 1 月 3 日
コメント済み: Ismaeel 2017 年 1 月 4 日
I wondered if there is a way to plot random points I know the total number of them, and then plot the lines that connect them. The connectivity matrix (A) is known and it has a size of nXn, where n is the total number of points and all elements of A are either 0 (no connection) or 1 (connected). If possible, I want to limit the dimension of graph by (1 X sqr(2)) unit of plot. Thanks in advance

採用された回答

Roger Stafford
Roger Stafford 2017 年 1 月 3 日
編集済み: Roger Stafford 2017 年 1 月 3 日
Assume that A is symmetric with zeros on the diagonal (bidirectional connections) and that there are n points.
x = rand(n,1);
y = sqrt(2)*rand(n,1);
plot(x,y,yo)
hold on
for i1 = 1:n-1
for i2 = i1+1:n
if A(i1,i2) ~= 0
plot([x(i1),x(i2)],[y(i1),y(i2)],y-)
end
end
end
hold off
axis equal
  1 件のコメント
Ismaeel
Ismaeel 2017 年 1 月 3 日
Thank you so much Roger; easy code and elegant and works as I want. Appreciate it...

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 1 月 3 日
When you have a connectivity matrix, you can create a graph() and plot() it; https://www.mathworks.com/help/matlab/ref/graph.plot.html

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by