This problem is inspired from problem 2405 https://www.mathworks.com/matlabcentral/cody/problems/2405-the-number-of-ways.
Check that problem description first for better understanding. Input p is the number of rows and n the number of columns of matrix A. Matrix A is filled with numbers from 1 to n*p in a row-wise fashion. Example:
n=2; p=2 A = [1 2 3 4];
The objective is to move through the matrix starting from the top row (at any position) and end at bottom row (at any position). The difference with problem 2405 is that here the movement is limited. You can not jump from, say, 3rd column of 1st row to 1st column of 2nd row. If you are at j-th column of i-th row, the only valid next movements are (i+1,j-1), (i+1,j) and (i+1,j+1). That means, you can only move vertically or diagonally (in both direction). No wrap around the edges is assumed (will be in next problem). Just like the previous problem, return a matrix where each row is a possible move.
For the example given above, the possible moves are (1,3), (1,4), (2,3) and (2,4). Thus the output matrix is
[1 3 1 4 2 3 2 4]