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

조회 수: 8 (최근 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

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by