如何对矩阵进行插值以获得特定值 [英] How to interpolate matrix to get specific values

查看:57
本文介绍了如何对矩阵进行插值以获得特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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,-1、0、1、2之类的点,必须在坐标值之间插入哪一步或2 ^ k-1个插值.>

谢谢!

解决方案

由于没有2D数据,因此仅在一个维度上进行插值,因此只需要函数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];

The first row represents the location of the values following X coordinates. I shift the first row by -0.63, so x becomes:

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];

How can I use interpolation to get the values at specific coordinates of the New_x matrix that we have in the x matrix? ([-2 -1 0 1 2] points)

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 .. ?];

I want to get the '?' values. I tried to use interp2 function but I don't know which step or 2^k-1 interpolated points between coordinates values I have to have in order to get the points like -2, -1, 0, 1, 2.

Thanks !

解决方案

Since you do not have 2D data, you are only interpolating on one dimension, you only need the function interp1.

This function can work on vector or matrices if necessary, but it require a slight reorganisation of your data.

%% 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

At this point you have your new xn and yn values in columns:

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

I would keep them like that if you have more operations to do on them later on. However, if you want it back into the format you had at the beginning, we can simply rebuild the new full matrix:

%% 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

这篇关于如何对矩阵进行插值以获得特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆