interp1(x,y,xq) in Matlab when x,xq: numeric array, and y: string array
interp1(x,y,xq) in Matlab when x,xq: numeric array, and y: string array
当x,xq
是数值数组,y
是字符串数组时,有没有办法将interp1
函数用作interp1(x,y,xq)
?
或者你能想到一个等效的向量方法吗?
虽然我不想转换为 table 并使用 tablelookup
。
示例:
x = [1, 3, 4, 7, 8];
y = ["A", "A", "B", "C", "C"];
xq = [2.5, 5, 6.7];
我假设您需要某种最近邻或有序插补。在这种情况下,您可以使用 interp1
但插值到源数组的 索引 上,然后使用结果索引到源值中。
>> x = [1 3 4 7 8];
>> y = ["hi" "hello" "bonjour" "hallo" "hola"];
>> xq = [2 5 6];
>> yq = y(interp1(x,1:length(x),xq,'nearest'))
yq =
1×3 string array
"hello" "bonjour" "hallo"
当x,xq
是数值数组,y
是字符串数组时,有没有办法将interp1
函数用作interp1(x,y,xq)
?
或者你能想到一个等效的向量方法吗?
虽然我不想转换为 table 并使用 tablelookup
。
示例:
x = [1, 3, 4, 7, 8];
y = ["A", "A", "B", "C", "C"];
xq = [2.5, 5, 6.7];
我假设您需要某种最近邻或有序插补。在这种情况下,您可以使用 interp1
但插值到源数组的 索引 上,然后使用结果索引到源值中。
>> x = [1 3 4 7 8];
>> y = ["hi" "hello" "bonjour" "hallo" "hola"];
>> xq = [2 5 6];
>> yq = y(interp1(x,1:length(x),xq,'nearest'))
yq =
1×3 string array
"hello" "bonjour" "hallo"