Generating the Ulam Spiral

This short spiral(n) function is supposed to generate a number spiral centered at one. For the most part, I understand the code but I can't seem to grasp why the polynomial m^2-m+1 is used.
function S = spiral(n)
%SPIRAL SPIRAL(n) is an n-by-n matrix with elements
% 1:n^2 arranged in a rectangular spiral pattern.
S = [];
for m = 1:n
S = rot90(S,2);
S(m,m) = 0;
p = m^2-m+1;
v = (m-1:-1:0);
S(:,m) = p-v';
S(m,:) = p+v;
end
if mod(n,2)==1
S = rot90(S,2);
end
Any ideas?

 채택된 답변

Prince
Prince 2012년 4월 30일

0 개 추천

Figured it out... the quadratic designates the values along the main diagonal.
For any future searchers

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2012년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by