Generating index from end and start index
显示 更早的评论
I am looking for an efficient way to do the following (without a loop so that large array can be handled efficiently). Any idea ?
A=rand(1,30) % some data from which I want to extract a subset
s=[1 5 10 22]; % start indexes of the subsets I want to extract
e=[3 7 17 25]; % end indexes of the subset I want to extract
R=[];
for i=1:length(s)
R=[R [s(i):e(i)]];
end
S=A(R); % the answer I am looking for, i.e. the subset of data I want to
% extract
1 个评论
per isakson
2012-9-10
编辑:per isakson
2012-9-10
- Did you analyze your code with the function, profile?
- Which version of Matlab do you use?
采纳的回答
更多回答(2 个)
Oleg Komarov
2012-9-10
1 个投票
Use the mex routine, very fast and reliable.
Azzi Abdelmalek
2012-9-10
A=rand(1,30) ;
s=[1 5 10 22];
e=[3 7 17 25];
l=repmat(1:4,2,1);
idx=eval(['[' sprintf('s(%d):e(%d) ',l(:)') ']'])
S=A(idx)
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!