필터 지우기
필터 지우기

Looping problem need guide.

조회 수: 2 (최근 30일)
reez all
reez all 2012년 2월 24일
편집: Cedric 2013년 10월 23일
how to use loop that the number of edges for output is equal as the input for the coding below:
clear;
clc;
nodes=6;
edges=8;
cnt = 0;
DG = zeros(nodes);
ancs = zeros(nodes); % matrix of ancestors
childs = zeros(nodes); % matrix of children
for e=1:edges
i=randi(nodes-1,1); % source node for new edge
j=randi([i+1,nodes],1); % target node for new edge
if sum(sum(DG(logical(ancs(j,:)),logical(childs(i,:)))))==0 && ...
sum(sum(DG(logical(ancs(i,:)),logical(childs(j,:)))))==0 && ...
sum(DG(logical(ancs(i,:)),j))==0 && sum(DG(i,logical(childs(j,:))))==0
% add edge
DG(i,j)=1;
% update ancestor and children relationship in the graph
ancs(j,i)=1;
ancs(j,:)=ancs(j,:)|ancs(i,:);
childs(i,j)=1;
childs(i,:)=childs(i,:)|childs(j,:);
end
end
edge=sum(sum(DG));
disp(edge);
i try to use while loop but still not work correctly. thank you.
  댓글 수: 2
Jan
Jan 2012년 2월 24일
@Reez all: Please do not send me emails. I answer every question I can answer. Trying to push me by emails does decrease my motivation to even read a question. Thanks.
It you really think that contacting the contributors directly is helpful, be sure to add a link to the question in your mail.
reez all
reez all 2012년 2월 24일
I apologise. I'm just think that you can help me because i see that you have wide knowledge related to the matlab. Sorry for interrupting you.

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

채택된 답변

Geoff
Geoff 2012년 2월 24일
So you mean that you want the number of 1's in DG to be equal to edges?
I would say that if-condition is preventing some edges from being added, and you are assuming by looping from 1:edges that it will be satisfied every time. Your two calls to randi do not guarantee that the same edge has not already been added. You say you used a while-loop... Was it similar to the following?
while sum(DG(:)) < edges
....
end
That would guarantee the end-result that you seem to be looking for. If the loop never terminates, then there is bound to be a problem with the logic in the if, which looks more convoluted than perhaps it needs to be.
-g-
  댓글 수: 1
reez all
reez all 2012년 2월 24일
thank you for your help, yes i need the number of 1's in DG to be equal to edges. i also have use the while loop that similar with the following code and as result the loop never terminates.
do you have any suggestion to improve my 'if' condition so that it can generate the same result?? The 'if' condition is full fill the Partially ordered set which is the main objective of my code. im really appreciate if you can help me to repair this code.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by