Matlab 中有 splat 运算符(或等效运算符)吗?

发布于 2024-12-26 06:12:36 字数 329 浏览 7 评论 0原文

如果我有一个数组(直到运行时长度未知),有没有办法调用一个函数,并将数组的每个元素作为单独的参数?

像这样:

foo = @(varargin) sum(cell2mat(varargin));
bar = [3,4,5];
foo(*bar) == foo(3,4,5)

上下文:我有一个 n-d 数组的索引列表,Q。我想要的是类似 Q(a,b,:) 的东西,但我只有 [a,b]。由于我不知道n,所以我不能只对索引进行硬编码。

If I have an array (of unknown length until runtime), is there a way to call a function with each element of the array as a separate parameter?

Like so:

foo = @(varargin) sum(cell2mat(varargin));
bar = [3,4,5];
foo(*bar) == foo(3,4,5)

Context: I have a list of indices to an n-d array, Q. What I want is something like Q(a,b,:), but I only have [a,b]. Since I don't know n, I can't just hard-code the indexing.

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

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

发布评论

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

评论(1

半窗疏影 2025-01-02 06:12:36

MATLAB 中没有运算符可以执行此操作。但是,如果您的索引(即示例中的 bar)存储在 元胞数组,那么你可以这样做:

bar = {3,4,5};   %# Cell array instead of standard array
foo(bar{:});     %# Pass the contents of each cell as a separate argument

{:} 创建一个 来自元胞数组的逗号分隔列表。除了覆盖 现有运算符(如图所示此处此处)以便它从标准数组生成逗号分隔的列表,或者创建您自己的类来存储索引并定义现有运算符如何对其进行操作(对于胆小的人来说,这两种选择都不是!)。

对于索引任意 ND 数组的特定示例,您还可以使用 sub2ind 函数(详见此处此处),但您最终可能会比上面的逗号分隔列表解决方案做更多的工作。另一种选择是自己计算线性索引,这会避开转换为元胞数组并仅使用矩阵/向量运算。这是一个例子:

% Precompute these somewhere:
scale = cumprod(size(Q)).';  %'
scale = [1; scale(1:end-1)];
shift = [0 ones(1, ndims(Q)-1)];

% Then compute a linear index like this:
indices = [3 4 5];
linearIndex = (indices-shift)*scale;
Q(linearIndex)  % Equivalent to Q(3,4,5)

There is no operator in MATLAB that will do that. However, if your indices (i.e. bar in your example) were stored in a cell array, then you could do this:

bar = {3,4,5};   %# Cell array instead of standard array
foo(bar{:});     %# Pass the contents of each cell as a separate argument

The {:} creates a comma-separated list from a cell array. That's probably the closest thing you can get to the "operator" form you have in your example, aside from overriding one of the existing operators (illustrated here and here) so that it generates a comma-separated list from a standard array, or creating your own class to store your indices and defining how the existing operators operate for it (neither option for the faint of heart!).

For your specific example of indexing an arbitrary N-D array, you could also compute a linear index from your subscripted indices using the sub2ind function (as detailed here and here), but you might end up doing more work than you would for my comma-separated list solution above. Another alternative is to compute the linear index yourself, which would sidestep converting to a cell array and use only matrix/vector operations. Here's an example:

% Precompute these somewhere:
scale = cumprod(size(Q)).';  %'
scale = [1; scale(1:end-1)];
shift = [0 ones(1, ndims(Q)-1)];

% Then compute a linear index like this:
indices = [3 4 5];
linearIndex = (indices-shift)*scale;
Q(linearIndex)  % Equivalent to Q(3,4,5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文