Hi.... Is my code true?

1 view (last 30 days)
helena
helena on 22 Oct 2014
Commented: Geoff Hayes on 22 Oct 2014
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 Comment
Geoff Hayes
Geoff Hayes on 22 Oct 2014
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).

Sign in to comment.

Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!