【问题标题】:How do I use matrix and compare to table and interpolate to closest values and generate new matrix?如何使用矩阵并与表进行比较并插入到最接近的值并生成新矩阵?
【发布时间】:2016-03-08 17:02:27
【问题描述】:

我有一个关于插值和比较一个矩阵与另一个矩阵的值,然后生成一个具有插值的新矩阵的问题。

我有一个包含时间戳、风速和方向的矩阵,如下所示:

Timestamp            Wind speed Direction
13-Apr-2000 00:10:00    9.285   265.59  
13-Apr-2000 00:20:00    7.044   261.32  
13-Apr-2000 00:30:00    6.578   258.66  
13-Apr-2000 00:40:00    7.476   261.43  
13-Apr-2000 00:50:00    6.918   260.29  
13-Apr-2000 01:00:00    6.832   253.48  
13-Apr-2000 01:10:00    6.368   250.11  
13-Apr-2000 01:20:00    5.279   260.44  
13-Apr-2000 01:30:00    5.27    266.75  

在我的另一个矩阵中,湍流 (TI) 取决于速度(向下)和方向(从左到右):

0        5        10     15       20     25
0   12.368  12.368  12.368  12.7585 13.149  13.149
1   12.368  12.368  12.368  12.7585 13.149  13.149
2   11.934  11.934  11.934  12.4135 12.893  12.893
3   11.726  11.726  11.726  11.917  12.108  12.108
4   11.391  11.391  11.391  11.065  10.739  10.739
5   11.32   11.32   11.32   11.0505 10.781  10.781
6   11.062  11.062  11.062  10.958  10.854  10.854
7   10.932  10.932  10.932  11.0905 11.249  11.249
8   11.244  11.244  11.244  11.294  11.344  11.344
9   12.037  12.037  12.037  11.757  11.477  11.477
10  11.934  11.934  11.934  11.8795 11.825  11.825

我想编写一个函数,其中我的输入是带有我的时间戳、风速和方向的矩阵。然后,我希望该函数考虑每个时间戳的每个风速和风向,然后插入到我的湍流矩阵中最接近的湍流值。

然后,我希望该函数生成一个新的时间序列(矩阵),其中包含与每个考虑的风速和风向的原始时间序列相同的时间戳的新湍流值。

我该怎么做?

我使用的是 MATLAB 2011b,但没有 SIMULINK。

【问题讨论】:

  • 你所说的插值是什么意思?找到最接近的值(例如,11.0505 用于 5.1 和 14)或(true)插值(对于相同的输入,它将返回类似 11.0578 的东西。)?

标签: matlab matrix interpolation


【解决方案1】:

我使用fit 函数对Tt 矩阵中的数据进行插值。

在开始时,它会寻找最接近的速度和方向的较低值。然后在我们正在寻找的点周围使用数据并拟合它们以z=ax+by+c 形式定义的表面。最后它评估函数到兴趣点。

% Data we have
Tt=[nan 0 5 10 15 20 25;
    0 12.368 12.368 12.368 12.7585 13.149 13.149;
    1 12.368 12.368 12.368 12.7585 13.149 13.149;
    2 11.934 11.934 11.934 12.4135 12.893 12.893;
    3 11.726 11.726 11.726 11.917 12.108 12.108;
    4 11.391 11.391 11.391 11.065 10.739 10.739;
    5 11.32 11.32 11.32 11.0505 10.781 10.781;
    6 11.062 11.062 11.062 10.958 10.854 10.854;
    7 10.932 10.932 10.932 11.0905 11.249 11.249;
    8 11.244 11.244 11.244 11.294 11.344 11.344;
    9 12.037 12.037 12.037 11.757 11.477 11.477;
    10 11.934 11.934 11.934 11.8795 11.825 11.825];
% Data we are looking for
Speed=4.5;
Direction=12.5;


[M,N]=size(Tt);

Sindex=find(Speed<Tt(:,1),1);     % find the index matching Speed
Dindex=find(Direction<Tt(1,:),1); % find the index matching Direction

if Sindex<=M&&Dindex<=N
   % Speed and Direction are defined in the Tt table
   S=[Tt(Sindex,1)*[1;1];Tt(Sindex+1,1)*[1;1]];
   D=[Tt(1,Dindex);Tt(1,Dindex+1);Tt(1,Dindex);Tt(1,Dindex+1)];
   T=[Tt(Sindex,Dindex);Tt(Sindex,Dindex+1);Tt(Sindex+1,Dindex);Tt(Sindex+1,Dindex+1)];
  % S,D,T are in form of [x1;x1;x2;x2],[y1;y2;y1;y2],[z11;z12;z21;z22] vectorized matrix.

  Tfit=fit([S,D],T,'poly11');  % get the linear fit of data, type help fit for more info

Turbulence=feval(Tfit,[Speed,Direction])  %Here we have the wanted Turbulence value.
else
  %What shall we do w... hen data are out of the matrix?
end

根据评论编辑

如果Tt矩阵的形式是[S,D,T],那么

Tfit=fit(Tt(:,1:2),Tt(:,3),'linearinterp');

将插入整个矩阵。

如果您有多个 Tt 矩阵,我建议您采用两步法。首先通过

创建你的TtDataBase.mat
save('TtDataBase.mat','Tfit1') % Run this in workspace for the first time only
save('TtDataBase.mat','Tfit2','-append') % Run this for the other Tfits

第一个创建/重写退出 .mat 文件,而第二个将现有新变量追加/重写到现有 .mat 文件。 我建议对每个Tfit 附加一些描述,比如有效范围等。

第二步可以使用

load(`TtDataBase.mat`,'Tfit2') % load Tfit2 only
load(`TtDataBase.mat`)         % load all variables in TtDataBase

如果您提供规范并找到正确的Tfit,您可以使用

load('TtDataBase.mat','description')
% decide which Tfit is good for the situation
% some code here

ProperFit='Tfit5'                 % Say the automated "logic" choose Tfit5 to be the best
load('TtDataBase.mat',ProperFit); % Tfit5 will appear in workspace
eval(['Tfit=' ProperFit])         % Tfit will appear in the workspace as a copy of Tfit5

Turbulence=feval(Tfit,[Speed,Direction]); % actual Tfit5 data will be used for interpolation.

如果你获得了新的Tt 矩阵并且你有描述变量,那么不要在使用save('TtDataBase.mat','description','description2','-append')之前忘记加载旧的描述并附加新的描述因为通过该命令,您正在重写现有文件。

【讨论】:

  • 辉煌的克劳利 - 非常感谢!我的表格实际上是一个包含三列的矩阵 - 对于我的问题中糟糕的可视化感到抱歉。您是否也碰巧对我如何为多个 Tt 矩阵执行此操作提出建议,然后将每个矩阵保存为单独的 TurbulenceTabe.mat 文件?假设我有 Tt1、Tt2、Tt3 等?
  • 然后将其标记为已回答。我强烈推荐最后一个例子。当speed 或direction 不在Tt 矩阵之外时小心。如果您有足够的数据,您还可以使用更平滑的插值。
  • 辉煌的克劳利 - 非常感谢!我的表格实际上是一个包含三列的矩阵 - 对于我的问题中糟糕的可视化感到抱歉。您是否也碰巧对我如何为多个 Tt 矩阵执行此操作提出建议,然后将每个矩阵保存为单独的 TurbulenceTable.mat 文件?假设我有 Tt1、Tt2、Tt3 等?
  • 我做了一些修改,希望能满足您的需求。
  • 非常好 - 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
相关资源
最近更新 更多