Given:
data = 0:10;
indices = [1 2; 3 7; 2 5];
Is there a one-line way to do this?:
for i = 1:length(indices)
out{i} = data(indices(i,1):indices(i,2))
end
Given:
data = 0:10;
indices = [1 2; 3 7; 2 5];
Is there a one-line way to do this?:
for i = 1:length(indices)
out{i} = data(indices(i,1):indices(i,2))
end
You can do this with arrayfun
:
out = arrayfun(@(a,b) data(a:b), indices(:,1), indices(:,2), 'UniformOutput', false);
However, internally arrayfun
is still probably just using a for loop, so I wouldn't expect to see any improvement in speed. This syntax simply allows you to write it as a one-liner. A somewhat ugly one-liner.
The following is on
out = arrayfun(@(ii)data(indices(ii,1):indices(ii,2)),1:size(indices,1),'UniformOutput',false)