Splitting a list into N parts of approximately equal length

前端 未结 30 1621
迷失自我
迷失自我 2020-11-22 16:16

What is the best way to divide a list into roughly equal parts? For example, if the list has 7 elements and is split it into 2 parts, we want to get 3 elements in o

30条回答
  •  春和景丽
    2020-11-22 16:57

    Rounding the linspace and using it as an index is an easier solution than what amit12690 proposes.

    function chunks=chunkit(array,num)
    
    index = round(linspace(0,size(array,2),num+1));
    
    chunks = cell(1,num);
    
    for x = 1:num
    chunks{x} = array(:,index(x)+1:index(x+1));
    end
    end
    

提交回复
热议问题