【问题标题】:How to interpolate matrix to get specific values如何插值矩阵以获得特定值
【发布时间】:2020-05-12 18:07:39
【问题描述】:

我在 MATLAB 中有这个矩阵:

x = [NaN  -2   -1  0    1    2;
      1  0.21 0.15 0.34 0.11 0.32;
      2  0.14 0.10 0.16 0.31 0.11];

第一行表示 X 坐标后面的值的位置。 我将第一行移动了 -0.63,所以 x 变为:

New_x = [NaN -2.63 -1.63 -0.63 0.37 1.37;
          1  0.21 0.15 0.34 0.11 0.32;
          2  0.14 0.10 0.16 0.31 0.11];

如何使用插值来获取我们在 x 矩阵中的 New_x 矩阵的特定坐标处的值? ([-2 -1 0 1 2]分)

New_xInterp = [NaN -2.63 .. -2 .. -1.63 .. -1 .. -0.63 .. 0 .. 0.37 .. 1 .. 1.37 .. 2;
                1   0.21 ..  ? ..  0.15 ..  ? ..  0.34 .. ? .. 0.11 .. ? .. 0.32 .. ?;
                2   0.14 ..  ? ..  0.10 ..  ? ..  0.16 .. ? .. 0.31 .. ? .. 0.11 .. ?];

我想得到“?”价值观。我尝试使用 interp2 函数,但我不知道在坐标值之间必须有哪一步或 2^k-1 个插值点才能获得像 -2、-1、0、1、2 这样的点。

谢谢!

【问题讨论】:

    标签: matlab matrix interpolation


    【解决方案1】:

    由于你没有二维数据,你只是在一维上插值,你只需要函数interp1

    如有必要,此函数可用于向量或矩阵,但需要对数据进行轻微重组。

    %% Input
    M = [NaN  -2   -1  0    1    2;
          1  0.21 0.15 0.34 0.11 0.32;
          2  0.14 0.10 0.16 0.31 0.11];
    
    %% Demultiplex inputs
    x = M(1,2:end).' ;        % extract X values, reorder in column
    y = M(2:end,2:end).' ;    % extract Y values, reorder in columns
    
    %% Interpolate
    xn = sort( [x-0.63 ; x] ) ;                         % Generate the new_x target values
    yn = interp1( x-0.63 , y , xn ,'linear','extrap') ; % Interpolate the full matrix in one go
    

    此时,您的列中有新的 xnyn 值:

    xn=         yn= 
    -2.63       0.21    0.14
    -2          0.1722  0.1148
    -1.63       0.15    0.1
    -1          0.2697  0.1378
    -0.63       0.34    0.16
    0           0.1951  0.2545
    0.37        0.11    0.31
    1           0.2423  0.184
    1.37        0.32    0.11
    2           0.4523  -0.016
    

    如果您以后对它们进行更多操作,我会保留它们。但是,如果您希望它恢复到开始时的格式,我们可以简单地重建新的完整矩阵:

    %% Rebuild global matrix
    Mout = [ M(:,1) , [xn.' ; yn.'] ] 
    
    Mout =
    NaN -2.63   -2      -1.63   -1      -0.63   0       0.37    1       1.37    2
    1   0.21    0.1722  0.15    0.2697  0.34    0.1951  0.11    0.2423  0.32    0.4523
    2   0.14    0.1148  0.1     0.1378  0.16    0.2545  0.31    0.184   0.11    -0.016
    

    【讨论】:

      【解决方案2】:

      也许你可以试试interp1 + arrayfun 如下所示

      r = sort([x(1,2:end),New_x(1,2:end)]);
      New_xInterp = [New_x(:,1),cell2mat(arrayfun(@(k) interp1(New_x(1,2:end),New_x(k,2:end),r),1:size(New_x,1),'UniformOutput',false).')];
      

      给了

      New_xInterp =
      
             NaN  -2.63000  -2.00000  -1.63000  -1.00000  -0.63000   0.00000   0.37000   1.00000   1.37000        NA
         1.00000   0.21000   0.17220   0.15000   0.26970   0.34000   0.19510   0.11000   0.24230   0.32000        NA
         2.00000   0.14000   0.11480   0.10000   0.13780   0.16000   0.25450   0.31000   0.18400   0.11000        NA
      

      上面的代码使用了线性插值。如果您需要其他选项,可以输入help interp1 以查看更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 2019-04-22
        • 1970-01-01
        • 2012-08-28
        • 1970-01-01
        相关资源
        最近更新 更多