使用向量作为矩阵的索引 [英] Use a vector as an index to a matrix
问题描述
我正在编写一个MATLAB函数来将数据读出到一个n维数组(可变维度大小)。我需要能够访问Matrix中的特定点(例如,写入或读取它),但我不知道要指定多少索引。
I'm writing a MATLAB function to read out data into an n-dimensional array (variable dimension size). I need to be able to access a specific point in the Matrix (to write to it or read it, for example), but I don't know ahead of time how many indexes to specify.
目前我有一个 current_point
向量,我迭代指定每个索引,并且 max_points
vector,指定数组的大小。所以,例如,如果我想要一个尺寸为1000×15×3, max_points = [1000 15 3]
和<$ c $的三维数组c> current_point 从 [1,1,1]
迭代到 [1000,15,3]
( [1,1,1]
- > [1000,1,1]
- > [1,2,1]
- > [1000,2,1]
- > ...)。我希望能够做的是将 current_point
作为矩阵的索引,如下所示:
Currently I have a current_point
vector which I iterate through to specify each index, and a max_points
vector which specifies the size of the array. So, if for example I wanted a 3-dimensional array of size 1000-by-15-by-3, max_points = [1000 15 3]
, and current_point
iterates from [1, 1, 1]
to [1000, 15, 3]
([1, 1, 1]
-> [1000, 1, 1]
-> [1, 2, 1]
-> [1000, 2, 1]
->...). What I'd like to be able to do is feed current_point
as an index to the matrix like so:
output_matrix(current_point) = val
但显然类似 output_matrix([1 2 3])= val
将只设置 outputmatrix(1:3)= 30
。我不能只使用虚拟变量,因为有时矩阵需要3个索引,其他时间4,其他时间2等,所以我需要一个可变长度的矢量。有一种使用一个向量作为点中的索引以简单的方式?
But apparently something like output_matrix([1 2 3]) = val
will just set outputmatrix(1:3) = 30
. I can't just use dummy variables because sometimes the matrix will need 3 indexes, other times 4, other times 2, etc, so a vector of variable length is really what I need here. Is there a simple way to use a vector as the points in an index?
推荐答案
使用功能 sub2ind
来创建线性索引是此问题的典型解决方案,如显示在此密切相关的问题中。您也可以自己计算线性索引,而不是调用 sub2ind
。
Using the function sub2ind
to create a linear index is the typical solution to this problem, as shown in this closely-related question. You could also compute a linear index yourself instead of calling sub2ind
.
但是,您的案例可能比我链接的其他问题更简单。如果你只是用你的 current_point
向量索引单个点(也就是说它只是你的n维下标的n元素向量)矩阵),然后您可以使用一个简单的解决方案,您可以使用函数 current_point 转换为下标的单元格数组/help/matlab/ref/num2cell.html 相对= nofollow noreferrer> num2cell
并使用它来创建以逗号分隔的索引列表。例如:
However, your case may be simpler than those in the other questions I linked to. If you're only ever indexing a single point with your current_point
vector (i.e. it's just an n-element vector of subscripts into your n-dimensional matrix), then you can use a simple solution where you convert current_point
to a cell array of subscripts using the function num2cell
and use it to create a comma-separated list of indices. For example:
current_point = [1 2 3 ...]; % A 1-by-n array of subscripts
subCell = num2cell(current_point); % A 1-by-n cell array of subscripts
output_matrix(subCell{:}) = val; % Update the matrix point
操作 subCell {:}
创建相当于输入 subCell {1},subCell {2},...
,这相当于输入 current_point(1 ),current_point(2),...
。
The operation subCell{:}
creates the equivalent of typing subCell{1}, subCell{2}, ...
, which is the equivalent of typing current_point(1), current_point(2), ...
.
这篇关于使用向量作为矩阵的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!