代码:
function Hilbertmatrix
%生成Hilbert扫描矩阵的主函数
Ho=[1 2;4 3];
flag=0;
k=input('Please input the integer k= ');
for i=1:k
if flag==0
H=[Ho,(4^(i+1)+1)*ones(2^i)-leftright(Ho);
(4^i)*ones(2^i)+Ho',(3*4^i+1)*ones(2^i)-leftright(Ho')];
else
H=[Ho,(4^i)*ones(2^i)+Ho';
(4^(i+1)+1)*ones(2^i)-updown(Ho),(3*4^i+1)*ones(2^i)-(leftright(Ho))'];
end;
flag=1-flag;
Ho=H;
end
['k=',num2str(k),'时,所得',num2str(2^(k+1)),'阶矩阵为'],H
function y=leftright(x)%该函数将一个矩阵左右颠倒
sizecolumn=size(x,2);
for j=1:sizecolumn
y(:,j)=x(:,sizecolumn+1-j);
end
function y=updown(x)%该函数将一个矩阵上下颠倒
sizerow=size(x,1);
for i=1:sizerow
y(i,:)=x(sizerow+1-i,:);
end
1