필터 지우기
필터 지우기

can I do summation of two columns of a text file ?

조회 수: 6 (최근 30일)
ND
ND 2016년 3월 5일
편집: Geoff Hayes 2016년 3월 5일
Please I have a text file contains two columns. I need to sum them and write the result and the first coloumn in a new text file.
Like this : text.txt =
0 0
1 2
2 5
-1 -2
The new text contains the first and third column only
0 0
1 3
2 7
-1 -3
Thanks

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 3월 5일
ND - you can try using uiimport to import the data from your text file into an appropriately named variable. When I tried this, using your sample data from above, I had to merge two of the cells to get -1. Once the data has been imported, you can then calculate the sum of the rows. For example, suppose the data is now in a variable as
data = [0 0
1 2
2 5
-1 -2];
You can determine the sum of each row as
rowSums = sum(data')';
You can then use save or fprintf to write the first column of data and rowSums to file.
  댓글 수: 1
ND
ND 2016년 3월 5일
편집: Geoff Hayes 2016년 3월 5일
Many thanks
I managed to do it like below,
data = dlmread('dd.txt');
rowsum = sum(data);
bb = [data(:,1) rowsum];
dlmwrite('dc.txt', bb)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by