如何对分类数据进行插值

How to interpolate on 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) 的值都可以接受 table。

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

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

by {'spring/summer'} 我的意思是 {'spring'}{'summer'} 但不是都像 {'spring/summer'}.

我是按照这个算出来的table:

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'

尝试以下操作:

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