Transpose the (0:N-1) vector, because you want a column vector, in order to be consistent with the shape of the data variable, which is also a column vector. With the transpose, the result is a column vector; without, it's a matrix, which causes the error later on.
cos(n*2*pi*f0*(0:N-1).'/T) % ^^ transpose
Same for the (1:N-1) vector a couple of lines later.