2017-05-28 60 views

回答

0

這是我現在該怎麼做了,但有一定是更優雅的方式吧?!

%% construct G 

% upper half 
firstDiagG = ones(h, 1); 
firstDiagG(h) = 0; 
firstDiagG = -firstDiagG; 
firstDiagG = repmat(firstDiagG,w,1); 

% first = diag(firstDiagG); 
firstG = spdiags(firstDiagG,0,n,n); 


secondDiagG = ones(h, 1); 
secondDiagG(h) = 0; 
secondDiagG = repmat(secondDiagG,w,1); 

% second = diag(secondDiagG); 
secondG = spdiags(secondDiagG,0,n,n); 
secondG = [zeros(size(secondG,1),1) secondG]; 
secondG = secondG(:,1:end-1); 

upperHalf = firstG + secondG; 

% lower half 
thirdDiagG = ones(n-h, 1); 
thirdDiagG = [thirdDiagG; zeros(h, 1)]; 
thirdDiagG = -thirdDiagG; 

% thirdG = diag(thirdDiagG); 
thirdG = spdiags(thirdDiagG,0,n,n); 

fourthDiagG = [ones(n-2*h, 1); zeros(n-2*h, 1)]; 

% fourthG = diag(fourthDiagG); 
fourthG = spdiags(fourthDiagG,0,n,n); 
fourthG = [zeros(size(fourthG,1), h) fourthG]; 
fourthG = fourthG(:,1:end-h); 
% fourthG = [fourthG zeros(size(fourthG,1),h)]; 
% fourthG = [fourthG; zeros(size(fourthG,1), size(fourthG,2))]; 

lowerHalf = thirdG + fourthG; 

G = [upperHalf; lowerHalf]; 

%% construct D 

% left half 
firstD = firstG;   % is the same as in G 

secondDiagD = secondDiagG; % is the same as in G 

% secondD = diag(secondDiagD); 
secondD = spdiags(secondDiagD,0,n,n); 
secondD = [zeros(1, size(secondD,1)); secondD]; 
secondD = secondD(1:end-1,:); 

leftHalf = firstD + secondD; 

% right half 
thrirdDiagD = flip(thirdDiagG); 

% thirdD = diag(thrirdDiagD); 
thirdD = spdiags(thrirdDiagD,0,n,n); 

fourthDiagD = ones(n, 1); 

% fourthD = diag(fourthDiagD); 
fourthD = spdiags(fourthDiagD,0,n,n); 
fourthD = [zeros(h, size(fourthD,1)); fourthD]; 
fourthD = fourthD(1:end-h,:); 

rightHalf = thirdD + fourthD; 

D = [leftHalf rightHalf];