6

我有一个 d 维数组A和长度等于 d的向量inds 。我想在inds访问A的值。

理想情况下,我会做类似A(*inds)的事情(借用 Python 的解包语法)。我不确定如何在 MATLAB 中执行此操作。

如果我做A(inds)我实际上从A中得到了 d 个单独的值,这不是我想要的。我想要的是inds的元素 i成为函数调用A () 中的第 i 个参数。

4

2 回答 2

13

一种解决方案是从下标索引向量中创建一个逗号分隔的列表inds。您可以使用NUM2CELL将其转换为元胞数组,然后{:}在索引时使用语法A

inds = num2cell(inds);
value = A(inds{:});
于 2011-06-17T19:45:34.580 回答
0

我认为这个工具可以帮助你:

如果你有一个像 R = rand(5,10,15,20) 这样的 ND 矩阵,并且你想访问特定模式中的元素,你可以使用 spindex 来访问与输入访问相同形状的输出。因此,如果您有 size(i1) = [5,5,5]、size(i2) = [5,5,5] 等,那么 size(spindex(R,i1,i2,i3,i4)) 也等于[5,5,5]。

%#example:

z = reshape(1:(5^4),[5,5,5,5]);
zid1 = [1,1,5];
zid2 = [1,2,5];
zid3 = [1,3,5];
zid4 = [1,4,5];
zOut = spindex(z,zid1,zid2,zid3,zid4)
%#   should be like [1,431,625]
zid1 = [1,2;3,4];
zid2 = [1,1;1,1];
zid3 = [1,1;1,1];
zid4 = [1,1;1,1];
zOut = spindex(z,zid1,zid2,zid3,zid4)
%%#    should be like [[1,2];[3,4]]

您需要将以下代码作为 spindex.m 添加到 MATLAB 路径中的某个位置。

 function outM = spindex(inM,varargin)
 %function outM = spindex(inM,varargin)
 %
 %returns a matrix indexed from inM via index variables contained in varargin
 %useful for retreiving multiple values from a large multidimensional matrix
 %
 %
 %inM is an N-d matrix
 %the index variables stored in varargin must be as numerous as the number of dimensions in inM
 %each index variable must be identical in size
 %
 %example:
 %
 %z = reshape(1:(5^4),[5,5,5,5]);
 %zid1 = [1,1,5];
 %zid2 = [1,2,5];
 %zid3 = [1,3,5];
 %zid4 = [1,4,5];
 %zOut = spindex(z,zid1,zid2,zid3,zid4)
 %%   should be like [1,431,625]
 %zid1 = [1,2;3,4];
 %zid2 = [1,1;1,1];
 %zid3 = [1,1;1,1];
 %zid4 = [1,1;1,1];
 %zOut = spindex(z,zid1,zid2,zid3,zid4)
 %%    should be like [[1,2];[3,4]]
 sz = size(inM);
 ndim = length(sz);
 if((ndim == 2) & (sz(2) ==1)) % ndim always returns at least 2
   ndim =1;
 end
 if(nargin ~= (ndim +1))
    extraDims = setdiff(1:(nargin - 1),1:ndim);
    for iExtraDim = extraDims
       if(any(varargin{iExtraDim}~=1))
          error('must have as many indicies as dimensions\n');
       end
    end
 end
 szid = size(varargin{1});
 for i = 1:ndim
    szid2 = size(varargin{i});
    if(any(szid2 ~= szid))
       error('indicies must have identical shape');
    end
    ndIdxs(:,i) = varargin{i}(:);
 end
 if(ndim == 1)
    idxs = ndIdxs(:,1);
 else
    idxs = myNDsub2ind(size(inM),ndIdxs);
 end
 outM = nan(1,length(idxs));
 outM(find(not(isnan(idxs)))) = inM(idxs(find(not(isnan(idxs)))));
 outM = reshape(outM,size(varargin{1}));





 function ndx = myNDsub2ind(siz,subs)
 %function ndx = NDsub2ind(siz,subs)
 %-------------------------------
 %works more smoothly when the dimensionality of the mtrx is unknown
 %siz should be like [10 10 4 5] if subs is like
 % 9 8 3 5
 % 1 1 1 1
 % 10 10 4 5
 % 5 8 3 3
 %
 % siz will be rotated for you if submit a row vec instead a col vector
 % example: NDsub2ind([10 10 4 5],[[9,8,3,5];[1,1,1,1]])
 %----------------------------------------------
 if(size(siz,1) > 1) && (size(siz,2) > 1)
    error('the siz variable must be a vector');
 end

 if((size(subs,1) ~= 1) && (size(subs,2) == 1))
    subs = subs';
 end
 siz = siz(:)';
 if length(siz)<2
         error('MATLAB:sub2ind:InvalidSize',...
             'Size vector must have at least 2 elements.');
 end

 if ((length(siz) ~= size(subs,2)))
     error('NDsub2ind: length(siz) must = size(subs,2)');
 end

 nPoints = size(subs,1);


 %Compute linear indices
 k = [1 cumprod(siz(1:end-1))];
 ndx = ones(nPoints,1);
 s = size(subs); %For size comparison
 for i = 1:length(siz),
     v = subs;
     fNaN = find(   (v(:,i) < 1) | (v(:,i) > siz(i))   );
     %Verify subscripts are within range
     v(fNaN,i) = nan;
     ndx = ndx + (v(:,i)-1)*k(i);
 end
于 2011-06-17T20:06:52.253 回答