2011-06-17 71 views
6

Tôi có mảng d chiều, A và vector inds có độ dài bằng d. Tôi muốn truy cập giá trị của A tại inds.MATLAB: Truy cập phần tử của mảng đa chiều có danh sách

Lý tưởng nhất, tôi sẽ làm điều gì đó như A (* inds) (mượn cú pháp giải nén từ Python). Tôi không chắc chắn làm thế nào để làm điều này trong MATLAB.

Nếu tôi làm A (inds) tôi thực sự nhận được d giá trị riêng biệt từ Một, mà không phải là những gì tôi muốn. Điều tôi muốn là cho phần tử i của số inds là tham số thứ i trong hàm gọi A().

+0

câu hỏi có thể có liên quan: http://stackoverflow.com/questions/2337126/how-do-i-do-multiple-assignment-in-matlab (và tất cả những người khác liên kết với nó) – Amro

Trả lời

13

Một giải pháp là tạo comma-separated list trong số vectơ chỉ mục chỉ số của bạn inds. Bạn có thể làm điều này bằng cách chuyển đổi nó vào một mảng tế bào sử dụng NUM2CELL, sau đó sử dụng cú pháp {:} khi lập chỉ mục A:

inds = num2cell(inds); 
value = A(inds{:}); 
+0

rực rỡ! cảm ơn! – faken

+0

cuối cùng là một số cách để giải nén một vectơ trong danh sách đối số ... –

0

Tôi nghĩ rằng công cụ này có thể giúp bạn:

nếu bạn có một ma trận NĐ như R = rand (5,10,15,20) và bạn muốn truy cập các phần tử trong một mẫu cụ thể, bạn có thể sử dụng spindex để truy cập đầu ra có cùng hình dạng như truy cập đầu vào. Vì vậy, nếu bạn có kích thước (i1) = [5,5,5], kích thước (i2) = [5,5,5], vv. Sau đó kích thước (spindex (R, i1, i2, i3, i4)) cũng bằng [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]] 

bạn sẽ cần thêm mã bên dưới dưới dạng spindex.m vào vị trí trong đường dẫn MATLAB của bạn.

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 
Các vấn đề liên quan