I am trying to find a more efficient way to shift the columns of the partitions of an array according to a defined slope and fill in the shifted regions with zeros I have done it below but, I know it could probably done in a way that manages memory better and is probably faster too. Memory comes in to play for me when the array is really large. See the example code below.

columns = 40;
rows = 9;
data = Table[i*100 + j, {i, rows}, {j, columns}];
dt = 2;
steplength = 5*dt;
steps = Floor[columns/steplength];
shifts = Floor[steplength/Abs[dt]];
partdata = Partition[data, {rows, steplength}];
Do[step[i] = partdata[[1]][[i]], {i, steps}];
Do[dtstep[i] = Partition[step[i], {rows, Abs[dt]}], {i, steps}];
Do[shiftdtstep[i][j] = ArrayPad[dtstep[i][[1]][[j]], {If[dt >= 0, {shifts - j, j - 1}, {j - 1, shifts - j}],{0,0}}], {i, steps}, {j, shifts}];
Do[shiftedstep[i] = ArrayReshape[Transpose[Table[shiftdtstep[i][j], {j, shifts}]], {rows + shifts - 1,Abs[dt]*shifts}], {i, steps}];
shifteddata = ArrayReshape[Transpose[Table[shiftedstep[i], {i, steps}]], {rows + shifts - 1, steps*steplength}];
data // MatrixForm
shifteddata // MatrixForm

The important thing for me is that the variable dt can be either positive or negative and any value I choose within reason of the original data.


This could be a rough short way you can build on:

datapart = Partition[Transpose[data], 10];
shifteddata2 = 
        datapart[[i]][[#]], {4 - Floor[#/2.1], Floor[#/2.1]}] & /@ 
      Range[10], {i, 1, 4}], 1]];

shifteddata2 === shifteddata
  • $\begingroup$ This works with the same data. However, if I change the number of columns to 20, I don't get the same results. Also, if a negative dt is given, it doesn't work at all. $\endgroup$ – Kane Dec 20 '14 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.