Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to read a data file line by line running a loop?

조회 수: 1 (최근 30일)
D M
D M 2016년 3월 23일
마감: MATLAB Answer Bot 2021년 8월 20일
Suppose I've a data file matrix.txt which has 1292 rows and 2 columns. I define the first column elements as x and the last as y. I need to do x*y for each line and then sum it to the x*y value obtained in the next line. So basically I need to run a loop where line-wise x,y will be read, x*y will be calculated and then in the next loop x*y will be summed up and finally summed x*y after 1292 iterations, will be displayed. How to do that? Kindly help.

답변 (1개)

Ced
Ced 2016년 3월 23일
I don't think you should read it line by line. Just read the whole thing, and process it line by line. 1300 rows are peanuts, memory or speed should not be an issue.
Looking at your actual process though, I would process the whole matrix at once. Maybe I misunderstood your calculations, but I don't see a reason for a loop (except for the cumulative sum in the end).
Probably fastest:
1. Read whole file
2. Compute x.*y for whole matrix
3. cumsum over results

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by