rgb2gray(reshape(TheVector, 32, 32, 3))
for any one vector of length 3072 (1024 x 1024 x 3). The result would be 2D, 32 x 32 . You could put a bunch of those together either along the third or fourth dimension, 32 x 32 x N or 32 x 32 x 1 x N.
Now, if you had a vector that was a multiple of 3072 long, representing different images, then it would start to get slightly interesting to do all of them at the same time without a loop.
temp = reshape(TheVector, 32, 32, 3, 1, []);
result = temp(:,:,1,1,:) * 0.299 + temp(:,:,2,1,:) * 0.587 + temp(:,:,3,1,:) * 0.114;
This would be 32 x 32 x 1 x N grayscale.