Miscellaneous Toolkit - zigzag


Function File: zigzag (mtrx)

Returns zigzag walk-off of the elements of mtrx. Essentially it walks the matrix in a Z-fashion.

mat = 1 4 7 2 5 8 3 6 9 then zigzag(mat) gives the output, [1 2 4 7 5 3 6 8 9], by walking as shown in the figure from pt 1 in that order of output. The argument mtrx should be a MxN matrix

An example of zagzig use:

 
 mat = reshape(1:9,3,3);
 zigzag(mat)
 ans =[1   2   4   7   5   3   6   8   9]

 
 

See also: zagzig.