MATLAB repeat numbers based on a vector of lengths

后端 未结 6 1201
失恋的感觉
失恋的感觉 2021-02-08 09:32

Is there a vectorised way to do the following? (shown by an example):

input_lengths = [ 1 1 1 4       3     2   1 ]
result =        [ 1 2 3 4 4 4 4 5 5 5 6 6 7 ]         


        
6条回答
  •  借酒劲吻你
    2021-02-08 10:10

    A fully vectorized version:

    selector=bsxfun(@le,[1:max(input_lengths)]',input_lengths);
    V=repmat([1:size(selector,2)],size(selector,1),1);
    result=V(selector);
    

    Downside is, the memory usage is O(numel(input_lengths)*max(input_lengths))

提交回复
热议问题