Hi.... Is my code true?

조회 수: 1 (최근 30일)
helena
helena 2014년 10월 22일
댓글: Geoff Hayes 2014년 10월 22일
Is my code true?(it is for finding optimization answer,the main problem is in the follow)
function A=string(s)
n=12;
best=0;
i=n/2;
j=n/2;
check=zeros(n,n);
%empty matrix
A=zeros(12,12);
%A(:,:)=[];
s=[2 1 2 1 1 2 1 1 2 1 2 1 2 1 1 ];
len=length(s);
A(i,j)=s(1);
check(i,j)=1;
for l=2:len
y=randperm(4);
switch y(1)
case 1
j=j-1;
case 2
i=i-1;
case 3
i=i+1;
case 4
j=j+1;
end
if check(i,j)==0
A(i,j)=s(l);
check(i,j)=1;
switch y(2)
case 1
j=j-1;
case 2
i=i-1;
case 3
i=i+1;
case 4
j=j+1;
end
if check(i,j)==0
A(i,j)=s(l);
check(i,j)=1;
end switch y(3)
case 1
j=j-1;
case 2
i=i-1;
case 3
i=i+1;
case 4
j=j+1;
end
if check(i,j)==0
A(i,j)=s(l);
check(i,j)=1;
end
end
end
%number of zero
num1=nnz(s);
num2=len-num1;
% andis of zero
w=find(A==0);
for z=1:num2
if w(i+1)==w(i)==2
best=best+1;
end
end
best
A
end
the string includes 0 and 1:
with 2 conditions:the nodes can be in four directions in a matrix & the edges connect 2 nodes dont cut each other & nodes(0&1) dont put on each other. The value of optimization calculate with couple node 0 and the distance between 2 couple nodes is 1. for example for string s1 =
and its graph is= red nodes show 1 , black nodes show 0 and blue lines show couple nodes.
in my code I replace 2 instead of zero.I dont understand how to find edge for writing optimization??
Thanks for all answers...
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2014년 10월 22일
Helena - it may be worthwhile to add some comments to your code so that we can see how the lines of code relate to the above problem. Once you have done that, consider attaching the revised version (the m file) to your question using the paperclip button (rather than pasting in the 100+ lines like you have done above).

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by