Azzera filtri
Azzera filtri

matrix concatenation

1 visualizzazione (ultimi 30 giorni)
Baba
Baba il 21 Feb 2012
Modificato: Anusha il 19 Ott 2013
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?

Risposta accettata

Walter Roberson
Walter Roberson il 21 Feb 2012
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!
  4 Commenti
Walter Roberson
Walter Roberson il 21 Feb 2012
Sorry, dropped an important parameter. I have edited it in to the above.
Baba
Baba il 21 Feb 2012
Perfect thank you!

Accedi per commentare.

Più risposte (1)

Sean de Wolski
Sean de Wolski il 21 Feb 2012
Do you want the third column to have 100 values the same?
If so:
A = [c1,c2,repmat(c3,100,1)];
If not:
A = [{c1} {c2} {c3}]
for more info:
doc cell
  3 Commenti
Baba
Baba il 21 Feb 2012
that one value should not repeat, just appear in the first row.
Walter Roberson
Walter Roberson il 21 Feb 2012
In order to do it with dlmwrite(), you would have to make two dlmwrite() calls, with the second using the -append option.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by