Creation of new column

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

 Accepted Answer

Paulo Silva
Paulo Silva on 20 Mar 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 Comments

Pap
Pap on 20 Mar 2011
Many thanks for your reply Paulo.
Since the above is en exanple of a huge txt. file (almost 10,800,000 rows), is there any different way to define a, except for writting its elements ?
Does "(1:end-1)" incorporate application of sum in previous rows?
Also can the above apply for logical functions (elseif, switch etc)?
Many thanks again,
Panos
I used the variable a with your example data, it should be what you get from the txt file.
What my code does is:
1- Obtain the sum of all the lines, put 0 at the first element of the vector to "shift down" the sum (what you requested).
2- Make the array where first three columns are the original array and the last one is the sum, last value of the sum vector is ignored for obvious reasons (see Walter answer)
You can do whatever you want to the data, just be careful because if you are really working with 10,800,000 rows some operations might take large amounts of memory and time to be performed.
Pap
Pap on 20 Mar 2011
Thanks again for your reply.
Iam also trying to examine a logical statement ( or equally a "switch" ) to simillar data, as folows:
1 2 5
2 3 6
3 4 4
4 1 4
6 4 8
5 2 9
2 5 5
I want to create the fourth column taking the value 'BUY' if value of third column of same row is greater than value in column three of previous row, otherwise is 'SELL'. If value in column three is equal to column three of previous row and previous row value of column four is 'BUY', then the current row value of collumn four is also 'BUY'. For this purpose, I arbitrarily define value on first row on column four equal to 'BUY".
So the new file should look like:
1 2 5 BUY
2 3 6 BUY
3 4 4 SELL
4 1 4 SELL
6 4 8 BUY
5 2 9 BUY
2 5 5 SELL
Could you please help me on creating this code?
Also how you define an element on previous rows of specific columns ?
Thank in advance,
Panos
a=[1 2 5
2 3 6
3 4 4
4 1 3
6 4 8
5 2 9
2 5 5]
b=a(:,3); %put the 3 column of a in a vector
c=[0;b(1:end-1)]; %make another vector from the previous one but "shifted down"
d=(b-c)>0; %compare both vectors
e=[a d]; %make the array with the results
The result is
e =
1 2 5 1
2 3 6 1
3 4 4 0
4 1 3 0
6 4 8 1
5 2 9 1
2 5 5 0
1 means BUY and 0 means SELL
Pap
Pap on 21 Mar 2011
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 on 21 Mar 2011
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)

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 20 Mar 2011

0 votes

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 Comment

Pap
Pap on 20 Mar 2011
Thanks for the answer Walter.
Actually I don't mind missing the final output line.
Panos

Sign in to comment.

Products

Tags

Asked:

Pap
on 20 Mar 2011

Community Treasure Hunt

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

Start Hunting!