Creation of new column
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have an input file (ex.txt) which contains three columns
0 0 0
1 2 5
2 3 6
3 4 4
4 1 3
6 4 8
5 2 9
2 5 5
I need to save the sum of the previous row columns into a fourth column in the input txt file, in order to look like
0 0 0
1 2 5 0
2 3 6 8
3 4 4 11
4 1 3 11
6 4 8 8
5 2 9 18
2 5 5 16
Any Hint?
Panos
0 comentarios
Respuesta aceptada
Paulo Silva
el 20 de Mzo. de 2011
%code
a=[0 0 0
1 2 5
2 3 6
3 4 4
4 1 3
6 4 8
5 2 9
2 5 5]
b=[0;sum(a,2)];
c=[a b(1:end-1)];
%result
c =
0 0 0 0
1 2 5 0
2 3 6 8
3 4 4 11
4 1 3 11
6 4 8 8
5 2 9 18
2 5 5 16
7 comentarios
Más respuestas (1)
Walter Roberson
el 20 de Mzo. de 2011
Hints: fopen, textscan, fprintf,
You will not be able to do this in a single output (not in any clean manner), as you cannot construct a numeric array that has a different number of columns.
You appear to be missing a final output line that has just a 12 by itself, representing the sum of the three values on the last line of the input file.
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!