Creation of new column

1 次查看(过去 30 天)
Pap
Pap 2011-3-20
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

采纳的回答

Paulo Silva
Paulo Silva 2011-3-20
%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 个评论
Pap
Pap 2011-3-21
Thanks Paulo,
How can I put in the above code the argument that if the value of current row (third column) is equal to the value of previous row and previous value of column 4 is 'BUY' then put BUY and if it is SELL ( otherwise) put 'SELL'.
Also where do you define 1,0 ( in order to change it to BUY SELL).
And may I also ask which code defines the first value only of cilumn 4, in order to apply then the above code.
Thanks a lot for the great help.
Pap
Pap 2011-3-21
Note that your data set is different from the above ( actually is my first one. The latest one has a case where two elements of column thre are equal and thus someone has to examine the previous value of column four)

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2011-3-20
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.
  1 个评论
Pap
Pap 2011-3-20
Thanks for the answer Walter.
Actually I don't mind missing the final output line.
Panos

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by