问题描述
我有一个d维数组 A 和长度为d的向量 inds .我想通过 inds 访问 A 的值.
I have a d-dimensional array, A, and vector inds with length equal to d. I would like to access the value of A at inds.
理想情况下,我会做类似 A(* inds)的事情(借用Python的拆包语法).我不确定如何在MATLAB中执行此操作.
Ideally, I'd do something like A(*inds) (borrowing the unpacking syntax from Python). I'm not sure how to do this in MATLAB.
如果我执行 A(inds),我实际上会从 A 中获得d个单独的值,这不是我想要的.我想要的是将 inds 的元素i用作函数调用 A ()中的第i个参数.
If I do A(inds) I actually get d separate values from A, which is not what I want. What I want is for element i of inds to be the ith parameter in the function call A().
推荐答案
一种解决方案是创建逗号分隔列表,该向量来自下标索引inds
的向量.您可以通过 NUM2CELL 将其转换为单元格数组来实现,然后在索引A
时使用{:}
语法:
One solution is to create a comma-separated list out of your vector of subscripted indices inds
. You can do this by converting it to a cell array using NUM2CELL, then using the {:}
syntax when indexing A
:
inds = num2cell(inds);
value = A(inds{:});
这篇关于MATLAB:使用列表访问多维数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!