MATLAB: Matrix concatenation

concatenate columns

I need to create a text file with 3 columns. first two columns are of the same size, 100. The 3rd column just has one number in it. How can I do that?

Best Answer

With col1, col2, and col3 being column vectors:
fmt = ['%f %f %f\n' repmat('%f %f\n', 1, 99)];
fid = fopen('YourOutputFile.txt', 'wt');
fprintf(fid, fmt, col1(1), col2(1), col3(1), [col1(2:end), col2(2:end)].');
fclose(fid);
Warning: the details of the fprintf() call depend upon col1 and col2 being column vectors!