is this code correct?

Write a MATLAB code that creates Magic Square of user defined odd order(for example, 5x5, 7x7, etc.) using Terrace method
1. Calculates elements of the Magic Square. 2. Displays the Magic Square.
Note : you can use the special fuction magic to test your own fuction only
M=zeros(n,n);
row=1;
column=(n+1)/2;
for k=1:n^2
M(row,column)=k;
row=row-1;
column=column+1;
if(row==0 && column<=n)
row=n;
elseif(row>0 && column>n)
column=1;
elseif((row==0 && column>n) || M(row,column)>0 )
row=row+2;
column=column-1;
end
end

댓글 수: 1

Jan
Jan 2011년 9월 28일
@redcavalry: Please delete your former thread, which contains a Pascal version of the code.

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

답변 (4개)

the cyclist
the cyclist 2011년 9월 28일

0 개 추천

I see that you have calculated the elements of the magic square (which look correct to me), but I do not see that you have displayed the magic square.
Daniel Shub
Daniel Shub 2011년 9월 28일

0 개 추천

you might want to try
type magic
TMW do it in 4 lines.

댓글 수: 2

the cyclist
the cyclist 2011년 9월 28일
Guessing that TMW doesn't use the "terrace method", but have to admit that I don't know for sure.
Walter Roberson
Walter Roberson 2011년 9월 28일
TMW needs 11 lines for the "singly even" case, at least in R2008b.
(Singly even is a size which is of the form 4*K+2 for non-negative integers K.)

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

Image Analyst
Image Analyst 2011년 9월 28일

0 개 추천

You can determine if your code is correct by subtracting your M from the correct answer. If everything is zero then you matched the correct magic square.
M % Display M
result = M - magic(n) % Display difference
Andrei Bobrov
Andrei Bobrov 2011년 9월 29일

0 개 추천

testing your code (edited)
all(diff([sum(M),sum(M,2)',sum(diag(M)),sum(diag(M(:,end:-1:1)))])==0) &&...
isequal(sort(M(:)'),1:numel(M))
ADDED
Hi redcavalry! You used siamese method. Terrace method is variant of siamese method. About of Terrace method in russian Wikipedia (see "Метод террас").
Cod: 1 variant (eg,for n = 5)
n = 5;
k = n*2-1;
B = zeros(k);
B(1:2:end) = 1;
a = hankel(1:k,k:-1:1);
B = (min(a,a(:,end:-1:1))>=n&B)+0;
A = zeros(n,k);
A(:,1:2:end) = 1;
A(~~A)=1:n^2;
A = reshape(A,k,[])';
B(~~B)=A(~~A);
i1 = [(n+1)/2:n-1,n+1:n+(n-1)/2];
i2 = [i1(end)+1:k,1:i1(1)-1];
B(:,i1) = B(:,i1) + B(:,i2);
B(i1,:) = B(i1,:) + B(i2,:);
M = B(i1(1):i1(end),i1(1):i1(end))
2 variant with use siamese method
i1 = (n+1)/2;
j1 = i1+1;
M = zeros(n);
for k = 1:n^2
M(i1,j1) = k;
i1 = i1 - 1;
j1 = j1 +1;
if i1 == 0 && j1 <= n
i1 = n;
elseif i1 > 0 && j1 > n
j1 = 1;
elseif i1 == 0 && j1 > n || M(i1,j1) > 0
j1 = rem(j1,n)+1;
i1 = i1 + 1;
end
end

댓글 수: 3

Walter Roberson
Walter Roberson 2011년 9월 29일
length(unique([sum(M),sum(M,2)',sum(diag(M)),sum(flipud(M))]))==1
Daniel Shub
Daniel Shub 2011년 9월 29일
I think to be a magic square the sorted elements have to equal 1:M
Andrei Bobrov
Andrei Bobrov 2011년 9월 29일
Yes, Daniel. Added.

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

카테고리

도움말 센터File Exchange에서 Repeated Measures and MANOVA에 대해 자세히 알아보기

태그

질문:

2011년 9월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by