我有一个Matlab矩阵M,大小为:[70 5 3 2 10 9 5 3 21]; 我有一个带有我想读取该矩阵坐标的向量: [5, 1, 1, 2, 3, 4, 1, 2, 1];
我想要得到的 MWE 示例:
M = rand(70 5 3 2 10 9 5 3 21);
coordinates = [5, 1, 1, 2, 3, 4, 1, 2, 1];
% Output desired:
M(5, 1, 1, 2, 3, 4, 1, 2, 1)
%Current attempt:
M(coordinates)
显然M(coordinates) <> M(5, 1, 1, 2, 3, 4, 1, 2, 1)
。有办法做到这一点吗?
最佳答案
这有点尴尬,但您可以将数组转换为元胞数组,然后转换为逗号分隔的列表:
M = rand(70, 5, 3, 2, 10, 9, 5, 3, 21);
coordinates = [5, 1, 1, 2, 3, 4, 1, 2, 1];
coords_cell = num2cell(coordinates);
result = M(coords_cell{:});
关于matlab - 给定沿每个轴的索引向量,获取矩阵值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71713041/