如何在 MATLAB 中使用查找表

发布于 2024-09-27 06:26:48 字数 775 浏览 4 评论 0原文

我需要对一组 2D 数组(如果需要,可以是 3D 矩阵)执行两个参数(一组:t,另一个来自数组)的指数运算。 f(t,x) = exp(tx) 然后我需要将第三维中每个值的结果相加。因为使用 bsxfun 执行整个操作需要花费太多时间,所以我考虑使用查找表。

我可以将表创建为矩阵 LUT(由于两个参数而为二维),然后我可以使用 LUT(par1,par2) 检索值。但是使用循环访问第三维也很昂贵。

我的问题是:有没有办法实现这种机制(查找表)以具有预定义的值,然后仅使用它们从矩阵元素(某种索引)访问而无需循环。或者,如何创建 MATLAB 自动处理的查找表以加速指数运算?

编辑: 我实际上使用类似的方法来创建 LUT。现在,我的问题实际上是如何以有效的方式访问它。

可以说我有一个二维数组M。对于这些值,我想将函数 f(t,M(i,j)) 应用为固定值 t。我可以使用循环来遍历 M 的所有值 (i,j)。但我想要一种更快的方法,因为我有一组 M,然后我需要将此过程应用于所有其他值。

我的函数比我给出的示例稍微复杂一点:

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

这是我的实际函数,正如您所看到的,它比我给出的示例更复杂。但想法是一样的。它对两个数组之差的指数的 M 组的第三维进行平均。

希望有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2024-10-04 06:26:48

我同意这个问题不是很清楚,显示一些代码会有所帮助。无论如何我都会尝试。

为了使 LUT 有意义,tx 获得的值集必须受到限制,例如整数。

假设指数可以是 -1000 到 1000 之间的任何整数,您可以像这样创建 LUT:

LUT = exp(-1000:1000);

创建索引(假设 t 是一维数组,x 是二维数组)

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);

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);
山有枢 2024-10-04 06:26:48

我不确定我是否理解你的问题,但我认为这就是答案。

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

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

LUT = (X+Y).^Z

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文