views:

53

answers:

2

I need to perform an exponential operation of two parameters (one set: t, and the other comes from the arrays) on a set of 2D arrays (a 3D Matrix if you want). f(t,x) = exp(t-x) And then I need to add the result of every value in the 3rd dimension. Because it takes too much time using bsxfun to perform the entire operation I was thinking of using a look up table.

I can create the table as a matrix LUT (2 dimensional due to the two parameters), then I can retrieve the values using LUT(par1,par2). But accessing on the 3rd dimension using a loop is expensive too.

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

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

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

My function is a little bit complex than the example I gave:

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

That is my actual function, as you can see is more complex than the example I presented. But the idea is the same. It does an average in the third dimension of the set of M's of the exponential of the difference of two arrays.

Hope that helps.

A: 

I am not sure I understand your question, but I think this is the answer.

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

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

LUT = (X+Y).^Z
MatlabDoug
+1  A: 

I agree that the question is not very clear, and that showing some code would help. I'll try anyway.

In order to have a LUT make sense at all, the set of values attained by t-x has to be limited, for example to integers.

Assuming that the exponent can be any integer from -1000 to 1000, you could create a LUT like this:

LUT = exp(-1000:1000);

Then you create your indices (assuming t is a 1D array, and x is a 2D array)

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

Finally, you create your result

output = LUT(indexArray);
%# sum along third dimension (i.e. sum over all `t`)
output = sum(output,3);
Jonas
Thanks, I used your solution with help of the sub2ind function. Sadly, using the LUT is slower than performing the operation with the bsxfun.
adn