The number of elements in X(:,j) is not equal to N*N, thats why you are getting error.
x = reshape(X(:,1),N,N) ;
See to it that the number of elements should not change. You may use imresize to achieve this.
X = imresize(X,[N*N,n]) ;