Matlab 中的数据分析

Data analysis in Matlab

我在 Matlab 中有一个时间向量,它没有一致的采样时间,例如。 t = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.9 3 3.1],我有另一个基于时间的向量 a = [2 5 2 4 5 7 8 0 10 1 0 25 6 14 5 2 7 98],当我绘制 (t,a) 时,有一条直线连接两个采样时间较大的点,我怎样才能消除这些间隙采样时间不一致,跳到更大的值?我知道在 0.71.3 之间以及在 ta 中的 22.9 之间定义相同间隔的 NaN 可能会有所帮助,但是如何区分采样时间是否变化?

也许你可以试试下面的代码,这里有两种方法可以实现:

  • 方法 1:添加 nan
clc;
clear;
close all;

t = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.9 3 3.1]
a = [2 5 2 4 5 7 8 0 10 1 0 25 6 14 5 2 7 98]

dt = diff(t);
idx = find(dt > mode(dt));
tC = mat2cell(t',[idx(1),diff([idx,length(t)])]);
aC = mat2cell(a',[idx(1),diff([idx,length(t)])]);
nadd = dt(idx)/mode(dt);
T = [];
A = [];
for i = 1:length(nadd)
  T = [T; tC{i};ones(int32(nadd(i)),1)*nan];
  A = [A; aC{i};ones(int32(nadd(i)),1)*nan];
endfor
T = [T;tC{end}];
A = [A;aC{end}];

plot(T,A)

  • 方法 2:将向量除以区间
clc;
clear;

t = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.9 3 3.1]
a = [2 5 2 4 5 7 8 0 10 1 0 25 6 14 5 2 7 98]

dt = diff(t);
idx = find(dt > mode(dt));
tC = mat2cell(t',[idx(1),diff([idx,length(t)])]);
aC = mat2cell(a',[idx(1),diff([idx,length(t)])]);

hold on;
arrayfun(@(k) plot(tC{k},aC{k}),1:(length(idx)+1));