I modified according to Justin and Jan's suggestions. And also added support for row vector. It is much faster now. See the updated file at http://www.mathworks.com/matlabcentral/fileexchange/47055-cell2csv-m
credits go to Sylvain.
Even though the code is lightning fast, the solution is not the proper one, hence this code is useless. See http://i.imgur.com/VNY73l7.png for example output of k = 6 for a naive (and very slow) implementation of the algorithm, and this submission. Obviously the naive is correct, this submission is incorrect.
Nonetheless thanks for the effort! It would be great if you could produce correct code that is still as fast.