I have two vectors x and n of equal length, and I want to create an array of arrays y from x, where the lengths for the subarrays of y corresponds to the elements of n.
For example, x=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8] and n=[3,1,4]. Then y=[[0.1,0.2,0.3],[0.4],[0.5,0.6,0.7,0.8]].
Is there a nice/fast way to do this? I tried searching the forum, but I couldn’t find anything directly relevant.
In Python (with NumPy) this would work:
indexPoints=np.cumsum(n[0:-1]);
y=np.split(x,indexPoints,axis=0);
My current stopgap in Julia:
nCumSum=cumsum(n);
numbTotal=nCumSum[end];
indexFirst=copy(nCumSum);
indexFirst[2:end]=indexFirst[1:end-1].+1;indexFirst[1]=1;
indexSecond=copy(nCumSum);
y=[(x[indexFirst[ii]:indexSecond[ii]]) for ii in 1:length(n)];
Update: I had a couple of typos in the Julia code and a mistake in the Python code.