问题
I am trying to implement a lookup table kind of thing in MATLAB.
I have data generated from a script with three variables swept, let's say var_a, var_b, var_c
. These are nested sweep, (var_a
-> var_b
-> var_c
)
And there are 10 outputs, out_01, out02, ..., out10
.
Now I have arranged the each output as out_01 = f(var_a,var_b,var_c)
, i.e., simply rearranging the data similar to nested loop.
My question is, how can I build a lookup table for such data?
I will give input like get out_01
@ certain var_a(X), var_b(Y), var_c(Z)
.
I have tried the following.
idx1_var_a = max(find(data.var_a <= options.var_a));
idx2_var_a = min(find(data.var_a >= options.var_a));
idx1_var_b = max(find(data.var_b <= options.var_b));
idx2_var_b = min(find(data.var_b >= options.var_b));
idx1_var_c = max(find(data.var_c <= options.var_c));
idx2_var_c = min(find(data.var_c >= options.var_c));
Y1 = interpn(data.var_c,data.var_b,data.var_a,data.out_01,data.var_c(idx1_var_c),data.var_b(idx1_var_b),data.var_a(idx1_var_a))
Y2 = interpn(data.var_c,data.var_b,data.var_a,data.out_01,data.var_c(idx2_var_c),data.var_b(idx2_var_b),data.var_a(idx2_var_a))
if Y1 == Y2
Y = Y1
else
Here I am unable to figure how to interpolate between these two output values,Y1, and Y2!!
end
Any help is welcome.
回答1:
I think you are looking for this:
Suppose you have:
var_a = 1:3;
var_b = 0:0.3:0.9;
var_c = 1:2;
[A, B, C] = ndgrid(var_a, var_b, var_c)
F = A.^3+B.^2+C;
Now you can directly acces the function at all existing points:
F(1,2,2)
Or alternatively
F(var_a==1,var_b==0.3,var_c==2)
Now if you are interested in values between the gridpoints, you can use interp3
Vq = interp3(F,1.5,2.5,1.5)
Note that this takes the desired location in the vector as input.
来源:https://stackoverflow.com/questions/19850762/lookup-table-in-matlab