如何在分类数据上进行插值

时间:2015-06-05 05:50:10

标签: string matlab interpolation string-interpolation categorical-data

我需要插入数据。对于浮点数,我通常使用

interp1(given_time,given_data,uniform_time)

但是,在这种情况下,我的given_data是一个字符串数组:

time=[1,5,18,30,40,42,47,54,64]

season=[{'winter'},{'winter'},{'winter'}, {'spring'}, {'spring'}, {'spring'}, {'summer'}, {'summer'}, {'summer'} ]

uniform_time=1:5:64

uniform_season= ????

输出的每个单元格必须是根据最近值uniform_time

的字符串

如何在matlab中实现它?

如果uniform_time(i)恰好位于time(j)time(k)之间,则season(j)season(k)的值均可接受。

在上面的案例中,我希望得到:

uniform_season= [{'winter'}, {'winter'}, {'winter'}, {'winter'}, {'winter'}, {'spring'}, {'spring'}, {'spring'}, {'spring/summer'}, {'summer'}, {'summer'}, {'summer'}, {'summer'}]

{'spring/summer'}我的意思是{'spring'}{'summer'},但不是两者都是{'spring/summer'}

我根据这张表计算了它:

new data: 1 6   11  16  21  26  31  36  41  46  51  56  61
closest old data: 1 5   18  18  18  30  30  40  41/42   47  54  54  64
string of old data: 'winter'    'winter'    'winter'    'winter'    'winter'    'spring'    'spring'    'spring'    spring/summer'  'summer'    'summer'    'summer'    'summer'

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

%// Inputs:
time=[1,5,18,30,40,42,47,54,64]
season=[{'winter'},{'winter'},{'winter'}, {'spring'}, {'spring'}, {'spring'}, {'summer'}, {'summer'}, {'summer'} ]
uniform_time=1:5:64

%// Change season to an ordinal list
[season_unique,~,season_int] = unique(season, 'stable')
idx = interp1(time, season_int, uniform_time, 'nearest-neighbour') %// Good chance you'll want to add the 'extrap' flag here as well
season_unique(idx)