How to use a lookup table in MATLAB

I need to perform an exponential operation with two parameters (one set: t, and the other from arrays) on a set of 2D arrays (if you want, a 3D matrix). f (t, x) = exp (tx) And then I need to add the result of each value in the 3rd dimension. Since it takes a long time to complete the whole operation bsxfun, I was thinking about using a lookup table.

I can create the table as a matrix LUT(two-dimensional due to two parameters), then I can get the values ​​with LUT(par1,par2). But access to the 3rd dimension using a cycle is also expensive.

My question is: is there a way to implement such a mechanism (lookup table) to have predefined values, and then just use them to access the matrix elements (kind of indexing) without loops. Or, how can I create a lookup table that MATLAB automatically performs to speed up the exponential operation?

EDIT: I actually used similar methods to create the LUT. Now my problem is how to access it in an efficient way.

Lets say I have a 2 dimensional array M. With the values ​​that I want to apply to the function f(t,M(i,j))for a fixed value t. I can use a loop to go through all the values ​​of (i, j) M. But I need a faster way to do this, because I have a set of M, and then I need to apply this procedure to all the other values.

My function is a bit more complicated than the example I gave:

pr = mean(exp(-bsxfun(@rdivide,bsxfun(@minus,color_vals,double(I)).^2,m)./2),3);

This is my actual function, as you can see, more complex than the example I presented. But the idea is the same. It has an average value in the third dimension of the set M of the exponent of the difference of two arrays.

Hope this helps.

+3
source share
2 answers

, , - . .

LUT , , t-x, , , .

, -1000 1000, LUT :

LUT = exp(-1000:1000);

(, t - 1D-, x - 2D-)

indexArray = bsxfun(@minus,reshape(t,[1,1,3]), x) + 1001; %# -1000 turns into 1

,

output = LUT(indexArray);
%# sum along third dimension (i.e. sum over all `t`)
output = sum(output,3);
+5

, , , .

x = 0:3
y = 0:2
z = 0:6

[X,Y,Z] = meshgrid(x,y,z)

LUT = (X+Y).^Z
0

Source: https://habr.com/ru/post/1769705/


All Articles