未知维矩阵的索引 [英] Indexing of unknown dimensional matrix
问题描述
我有一个非固定维矩阵M,我想从中访问单个元素. 元素的索引包含在向量J中.
I have a non-fixed dimensional matrix M, from which I want to access a single element. The element's indices are contained in a vector J.
例如:
M = rand(6,4,8,2);
J = [5 2 7 1];
output = M(5,2,7,1)
这次,M有4个维,但这是未知的.这取决于我正在编写的算法的设置.可能就是那样
This time M has 4 dimensions, but this is not known in advance. This is dependent on the setup of the algorithm I'm writing. It could likewise be that
M = rand(6,4);
J = [3 1];
output = M(3,1)
所以我不能简单地使用
output=M(J(1),J(2))
我当时正在考虑使用 sub2ind ,但这也需要使用变量以逗号分隔..
I was thinking of using sub2ind, but this also needs its variables comma separated..
@gnovice
这可行,但是我打算从矩阵M
中大量使用这种元素提取.因此,如果每次访问M
时都必须创建一个临时变量cellJ
,这会不会极大地减慢计算速度?
this works, but I intend to use this kind of element extraction from the matrix M
quite a lot. So if I have to create a temporary variable cellJ
every time I access M
, wouldn't this tremendously slow down the computation??
我也可以编写一个单独的函数
I could also write a separate function
function x= getM(M,J)
x=M(J(1),J(2));
% M doesn't change in this function, so no mem copy needed = passed by reference
end
,并将其调整为适用于算法的不同配置.当然,这是我在问题中没有提到的速度与灵活性的关系.
and adapt this for different configurations of the algorithm. This is of course a speed vs flexibility consideration which I hadn't included in my question..
但是:这仅可用于获取元素,用于设置除了实际使用索引(最好是线性索引)外没有其他方法.我仍然认为sub2ind是一个选择.我原本打算的最终结果是这样的:
BUT: this is only available for getting the element, for setting there is no other way than actually using the indices (and preferably the linear index). I still think sub2ind is an option. The final result I had intended was something like:
function idx = getLinearIdx(J, size_M)
idx = ...
end
结果:
function lin_idx = Lidx_ml( J, M )%#eml
%LIDX_ML converts an array of indices J for a multidimensional array M to
%linear indices, directly useable on M
%
% INPUT
% J NxP matrix containing P sets of N indices
% M A example matrix, with same size as on which the indices in J
% will be applicable.
%
% OUTPUT
% lin_idx Px1 array of linear indices
%
% method 1
%lin_idx = zeros(size(J,2),1);
%for ii = 1:size(J,2)
% cellJ = num2cell(J(:,ii));
% lin_idx(ii) = sub2ind(size(M),cellJ{:});
%end
% method 2
sizeM = size(M);
J(2:end,:) = J(2:end,:)-1;
lin_idx = cumprod([1 sizeM(1:end-1)])*J;
end
方法2的速度是方法1的20倍(要转换的少量索引集(=P
))到80倍(大量的索引集(=P
))快.易于选择
method 2 is 20 (small number of index sets (=P
) to convert) to 80 (large number of index sets (=P
)) times faster than method 1. easy choice
推荐答案
对于J
可以是任意长度的一般情况(我认为始终与M
中的尺寸数匹配),有几个选择你有:
For the general case where J
can be any length (which I assume always matches the number of dimensions in M
), there are a couple options you have:
-
您可以使用J的每个条目放置在单元格阵列的单元格中="nofollow noreferrer">
num2cell
函数,然后创建冒号运算符:
You can place each entry of
J
in a cell of a cell array using thenum2cell
function, then create a comma-separated list from this cell array using the colon operator:
cellJ = num2cell(J);
output = M(cellJ{:});
You can sidestep the sub2ind
function and compute the linear index yourself with a little bit of math:
sizeM = size(M);
index = cumprod([1 sizeM(1:end-1)]) * (J(:) - [0; ones(numel(J)-1, 1)]);
output = M(index);
这篇关于未知维矩阵的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!