首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >索引一组开始和结束索引的数组

索引一组开始和结束索引的数组
EN

Stack Overflow用户
提问于 2019-09-04 11:50:46
回答 3查看 156关注 0票数 2

我有两个数组:

代码语言:javascript
复制
timesteps = [1,3;5,7;9,10];
data = [1,2,3,4,5,6,7,8,9,10];

timesteps数组中的值描述了我想要的data值。第一列开始,第二列结束。

这里我想要[1,2,3,5,6,7,9,10]

所以这段代码对我来说很好,但是因为for循环很慢.在Matlab中是否有一个内线,这样我就可以摆脱反循环了吗?

代码语言:javascript
复制
newData=[];
for ind=1:size(timesteps,1)
  newData=cat(2,newData,data(timesteps(ind,1):timesteps(ind,2)));
end

编辑:通过Wolfie的解决方案,我得到了以下(非常好的)结果。(我只使用了一个小数据集,它通常是50倍大。)

代码语言:javascript
复制
(Mine)    Elapsed time is 48.579997 seconds.
(Wolfies) Elapsed time is 0.058733 seconds.
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-09-04 13:35:46

Irreducible's answer使用str2numsprintf在数值和字符数据之间切换以创建索引.(在我的测试中),这不像您已经对小数组所做的那样循环执行,但是对于大型数组来说,随着内存分配处理得更好,这就更快了。

您可以通过预先分配输出并对其进行索引来提高性能,以避免循环中的级联。对于大型数组,这可能会带来很大的速度。

代码语言:javascript
复制
N = [0; cumsum( diff( timesteps, [], 2 ) + 1 )];
newData = NaN( 1, max(N) );
for ind = 1:size(timesteps,1)
    newData(N(ind)+1:N(ind+1)) = data(timesteps(ind,1):timesteps(ind,2));
end

下面的基准说明了这是如何持续更快。

  • X轴:data中的元素数
  • Y轴:时间以秒为单位
  • 假设:选择索引的随机子集,其中index的行数比data少4倍。

标杆小区

注意,这是变量,取决于所使用的索引。在下面的代码中,我会随机生成每次运行的索引,所以您可能会看到这个图有一点跳跃。

但是,带预分配的循环总是更快,而没有预分配的循环总是以指数方式爆炸。

基准代码

代码语言:javascript
复制
T = [];
p = 4:12;
for ii = p
    n = 2^ii;
    k = 2^(ii-2);

    timesteps = reshape( sort( randperm( n, k*2 ) ).', 2, [] ).';
    data = 1:n;

    f_Playergod = @() f1(timesteps, data);
    f_Irreducible = @() f2(timesteps, data);
    f_Wolfie = @() f3(timesteps, data);

    T = [T; [timeit( f_Playergod ), timeit( f_Irreducible ), timeit( f_Wolfie )]];
end

figure(1); clf; 
plot( T, 'LineWidth', 1.5 );
legend( {'Loop, no preallocation', 'str2num indexing', 'loop, with preallocation'}, 'location', 'best' );
xticklabels( 2.^p ); grid on;

function newData = f1( timesteps, data )
    newData=[];
    for ind=1:size(timesteps,1)
      newData=cat(2,newData,data(timesteps(ind,1):timesteps(ind,2)));
    end
end
function newData = f2( timesteps, data )
    newData = data( str2num(sprintf('%d:%d ',timesteps')) );
end
function newData = f3( timesteps, data )
    N = [0; cumsum( diff( timesteps, [], 2 ) + 1 )];
    newData = NaN( 1, max(N) );
    for ind = 1:size(timesteps,1)
        newData(N(ind)+1:N(ind+1)) = data(timesteps(ind,1):timesteps(ind,2));
    end
end
票数 5
EN

Stack Overflow用户

发布于 2019-09-04 12:01:42

为了摆脱for循环,您可以执行以下操作:

代码语言:javascript
复制
timesteps = [1,3;5,7;9,10];
data = [1,2,3,4,5,6,7,8,9,10];
%create a index vector of the indices you want to extract
idx=str2num(sprintf('%d:%d ',timesteps'));
%done
res=data(idx)

res =

 1     2     3     5     6     7     9    10

然而,关于运行时,正如评论中所述,我还没有测试它,但我怀疑它是否会更快。这里唯一的优点是结果数组不必在每次迭代中更新.

票数 2
EN

Stack Overflow用户

发布于 2019-09-04 12:17:11

我通常会去做一个循环,但是你可以这样做

代码语言:javascript
复制
%take every 1st column element and 2nd column elemeent, use the range of numbers to index data
a=arrayfun(@(x,y) data(x:y),timesteps(:,1),timesteps(:,2),'UniformOutput',0) 
%convert cell array to vector
a=[a{:}]

我应该指出,这是比循环慢得多。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57787803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档