Search code examples
matlabregressionleast-squares

Limit the range of least square regression


I wanted to do a regression on a set of data A(k1,k2), however I wanted limit it regression within a range of -K1<k1<K1 and -K2<k<K2. A is an image phase consist of 60x60 matrix, with dimension of MxN. Least squares approximation to be done by 0.1N/2 from the center of normalized frequency area of A.

Here's the portion of the code:

A=rand(60);
[m, n]=size(A);
[M,N] = meshgrid(1:m,1:n);
X = [M(:), N(:)];
B=regress(A(:), X);  %regression will be done on all the values of A, part where adjustment needed
hat=reshape(X*B,m,n);

Solution

  • You can first select the subset of your matrix on which you want to perform the regression:

    % generate the full image
    A_full=rand(60);
    [m, n]=size(A_full);
    
    % select the part you want, 
    % it is not very clear to me if this is really the part you want, 
    % but I think you will be able to change it to your needs
    A=A_full(floor(m/2-0.1*m/2):ceil(m/2+0.1*m/2), floor(n/2-0.1*n/2):ceil(n/2+0.1*n/2)); 
    
    % perform the regression on the selected part of A (like you did it)
    [m, n]=size(A);
    [M,N] = meshgrid(1:m,1:n);
    X = [M(:), N(:)];
    B=regress(A(:), X);  %regression will be done on all the values of A, part where adjustment needed
    hat=reshape(X*B,m,n);