Translate Matlab Code into R ---- Prim's Algorithm

11 ビュー (過去 30 日間)
Furqan
Furqan 2014 年 12 月 15 日
コメント済み: per isakson 2014 年 12 月 16 日
Hi can someone help me to translate this Matlab code into R Studio Code.
function T = MinST(G)
%%Add comment here describing V1 and V2
V1 = [1];
V2 = 2:length(G);
%%T is the result, set to have no edges
T = zeros(size(G));
%%max is just a big number bigger than any edge weight in G
max = 10;
while (~isempty(V2))
%%* Execution trace: V1, V2, T
%%Add a comment here: what do the following loops do?
min = max;
for i=1:length(V1)
for j=1:length(V2)
if (G(V1(i),V2(j))>0 && G(V1(i),V2(j))<min)
%%add a comment here: what is the condition
%%and what do these lines do?
min = G(V1(i),V2(j));
u = V1(i);
v = V2(j);
end
end
end
%%* Execution trace: u, v, min
%%What does the following line do and do the invaraints
%%of the loops above ensure it is the right thing?
T(u,v) = min;
%%explain the following two lines
V1 = [V1 v];
V2(V2==v)=[];
end
end
  2 件のコメント
Furqan
Furqan 2014 年 12 月 15 日
function T = MinST(G)
%%Add comment here describing V1 and V2
V1 = [1]; V2 = 2:length(G);
%%T is the result, set to have no edges
T = zeros(size(G));
%%max is just a big number bigger than any edge weight in G
max = 10;
while (~isempty(V2))
%%* Execution trace: V1, V2, T
%%Add a comment here: what do the following loops do?
min = max;
for i=1:length(V1)
for j=1:length(V2)
if (G(V1(i),V2(j))>0 && G(V1(i),V2(j))<min)
%%add a comment here: what is the condition
%%and what do these lines do?
min = G(V1(i),V2(j));
u = V1(i);
v = V2(j);
end
end
end
%%* Execution trace: u, v, min %%What does the following line do and do the invaraints %%of the loops above ensure it is the right thing? T(u,v) = min; %%explain the following two lines
V1 = [V1 v];
V2(V2==v)=[];
end
Sorry this is the right format of code with saprate lines
per isakson
per isakson 2014 年 12 月 16 日

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

回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by