我需要插入数据。对于浮点数,我通常使用
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'
答案 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)