Skip to main content
4 of 5
edited body

Matlab, 119 bytes

I just tried my best to shorten my code in Matlab, like telegraphing it.

[m,n]=size(M); a=(1:m)'*ones(1,n); b=ones(m,1)*(1:n); A=a+b-1; B=a-b; C=(A.^2+(-1).^A.*B+1); [~,I]=sort(C(:)); V=M(:); V=V(I)'; 

Notes:

  1. M is an m×n matrix.
  2. a and b are both matrices same size of M, each row of a consists of numbers equal to its row number, while each column of b is equal to its column number. Thus, a+b is a matrix whose element equals to sum of its row and column number, i.e., matrix(p,q)=p+q.
  3. Thus, A(p,q)=p+q-1; and B(p,q)=p-q.
  4. C is mathematically stated as equation below. Zigzagifiedly increasing matrix with the equation, a zigzagifiedly increasing matrix can be made like shown below.
C = 1 2 6 7 3 5 8 14 4 9 13 18 10 12 19 25 
  1. C indicates the order of elements of M in zigzagified results. Then, [~,I]=sort(C(:));returns the order, i.e., I, thus, V=V(I)' is the result.