Indexing out of variable using () and {} are just shorthand for the function subsref, in exactly the same way that * is shorthand for mtimes. You can also call subsref directly, which offers one solution to your question, although not quite in one line: >> f = ones(10,30,5);
>> dim = 2;
Now lets get the data you want:
>> C = repmat({':'},1,max(dim,ndims(f)));
>> C{dim} = 3:2:size(f,dim)-2;
>> out = subsref(f,substruct('()',C));
and checking:
>> size(out)
ans =
10 13 5
This is likely more efficient than calling arrayfun, and has the advantage that it does not generate lists of all indices for all dimensions. You may possibly find some efficient solution with sub2ind. You could even put the code into a small function:
function out = mysub(f,dim)
C = repmat({':'},1,max(dim,ndims(f)));
C{dim} = 3:2:size(f,dim)-2;
out = subsref(f,substruct('()',C));
end
Best Answer