フィルターのクリア

Generating more "edges" randomly

1 回表示 (過去 30 日間)
Jonathan
Jonathan 2012 年 7 月 13 日
I created the code below to generate a matrix (m) with two columns (edgeStart and edgeEnd) that contain 100 random integers each. These integers are the "nodes" and each "edge" is a tuple of two distinct nodes. The start nodes are in the first column, and the end nodes are in the second column. Right now, each start node corresponds to only one end node (ie. there are 100 nodes with 100 edges between them). Is there a way to create more edges (ie. with 100 nodes, have about 250 edges between them)?
clear all
edgeStart = zeros(100,1);
for nodeStart = 1:100
edgeStart(nodeStart) = ceil(100.*rand(1));
edgeStart = edgeStart';
end
edgeEnd = zeros(100,1);
for nodeEnd = 1:100
edgeEnd(nodeEnd) = ceil(100.*rand(1));
edgeEnd = edgeEnd';
end
m = [edgeStart,edgeEnd];
csvwrite('csvtest.csv',m)
type csvtest.csv
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 7 月 14 日

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

採用された回答

Jonathan
Jonathan 2012 年 7 月 14 日
I figured it out myself. Here is the code:
clear all
numNodes = 100;
numEdges = 250;
edgeStart = zeros(numEdges,1);
for nodeStart = 1:numEdges
edgeStart(nodeStart) = ceil(numNodes.*rand(1));
end
edgeEnd = zeros(numEdges,1);
for nodeEnd = 1:numEdges
edgeEnd(nodeEnd) = ceil(numNodes.*rand(1));
end
sortedEdgeStart = sort(edgeStart,'ascend');
m = [sortedEdgeStart,edgeEnd];
dupCells = find(eq(sortedEdgeStart,edgeEnd))
duplicates = m(dupCells,:)
csvwrite('csvtest.csv',m)
type csvtest.csv

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by