Write to a .txt file
显示 更早的评论
Hello, I have to write a code that will do what the following question asks. I am very confused on how to do this.
'You will want to add up all of the vectors in the file called "Random_Data.txt", but in addition, you want to save all of the intermediate results. For example, once you add up the first two vectors, write the results into another text file called "Results.txt". The next line should have the intermediate answer of adding the previous result to the third vector, etc... Your Results.txt should contain vector_count - 1 vectors with the final vector being the final answer for adding all of the vectors up in the Random_Data.txt file.
Example:
Original File:
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Results.txt
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
any help is much appreciated!
回答(1 个)
Walter Roberson
2017-3-7
0 个投票
The quick way is to use cumsum() and write out the 2nd row to the end row.
2 个评论
Dewey Phillips
2017-3-8
编辑:Walter Roberson
2017-3-8
i tried this code and it did not work.
B = cumsum(my_ans)
C = B([2:end],:)
fprintf(fileID, '%d/t%d/t%d/t%d/t%d/n', C')
Walter Roberson
2017-3-8
\t and \n not /t and /n
类别
在 帮助中心 和 File Exchange 中查找有关 Share and Distribute Software 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!