2016-11-22 16 views
0

對於n維陣列的徑向距離內的indicies,我正在尋找一個inbuild函數執行以下操作(在3D情況中給出):Inbuild功能,以選擇離原點索引

for l = 1:size(dct, 1) 
     for m = 1:size(dct, 2) 
      for n = 1:size(dct, 3) 
       if sqrt(l*l + m*m + n*n) > r 
        break 
       end 
       new(end+1) = dct(l,m,n); 
      end 
     end 
    end 

回答

1

使用ndgrid和邏輯索引:

r = 5; % assume radius 

% make some fake data 
dctDim = [3 4 5]; 
dct = randn(dctDim); 

% make indexes 
[l, m, n] = ndgrid(1 : size(dct, 1), 1 : size(dct, 2), 1 : size(dct, 3)); 

% extract values from dct within r-radius of top-left of array 
new = dct(l.^2 + m.^2 + n.^2 <= r^2);