필터 지우기
필터 지우기

transform the while loop into a loop for

조회 수: 3 (최근 30일)
PIERRE OLIVIER SCHOUEL ATANGANA
댓글: Walter Roberson 2021년 10월 28일
Hello ,
I would like to turn the following block of code into a loop for
while feof(fid) == 0
tline = fgetl(fid);
TargetVar = regexp(tline,' ','split');
if length(TargetVar)>2
[xc, reste] =strtok(tline, ' ');
[yc, reste] =strtok(reste, ' ');
[zc, reste] =strtok(reste, ' ');
[Rayon, reste] =strtok(reste, ' ');
h=h+1;
Pts(h,1:3)= [str2num(xc) str2num(yc) str2num(zc)] ;
Ray(h,1)=str2num(Rayon);
Vol(h,1)=((4/3)*pi)*(Ray(h)^3);
end
end

채택된 답변

Image Analyst
Image Analyst 2021년 9월 1일
Try this:
maxLines = 9999999; % More than you expect to ever need.
for k = 1 : maxLines
tline = fgetl(fid);
if ~ischar(textLine)
% End of file so break out of loop.
break;
end
% The rest of your code follows (I didn't check it).
TargetVar = regexp(tline,' ','split');
if length(TargetVar)>2
[xc, reste] =strtok(tline, ' ');
[yc, reste] =strtok(reste, ' ');
[zc, reste] =strtok(reste, ' ');
[Rayon, reste] =strtok(reste, ' ');
h=h+1;
Pts(h,1:3)= [str2num(xc) str2num(yc) str2num(zc)] ;
Ray(h,1)=str2num(Rayon);
Vol(h,1)=((4/3)*pi)*(Ray(h)^3);
end
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 9월 1일
편집: Walter Roberson 2021년 9월 1일
This is not equivalent code. It will give out after roughly
lines = (9999999+1);
chars_per_line = 50;
max_file_size_estimate_gigabytes = lines * chars_per_line / 2^30
max_file_size_estimate_gigabytes = 0.4657
I can store files 1000 times larger than that on my system, without difficulty.
Only transform while loops into for loops if you have a maximum number of iterations (or fixed number of iterations): if you cannot put an upper bound on the number of iterations, then leave it as a while loop.
Image Analyst
Image Analyst 2021년 9월 1일
I agree that while is preferable. I just gave the for loop way because I guess he wanted it for comparison purposes.
Often people know how big their files are. Like if you know you're writing data for up to hundred files and each file will have 5 numbers for it, then in your output text file you won't have more than 100 lines in it and it won't be hundreds of GB. The comment says that you put the line count at way more than you know you'll ever need, and in that case, the code works.
maxLines = 9999999; % More than you expect to ever need.

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

추가 답변 (3개)

Walter Roberson
Walter Roberson 2021년 9월 1일
for is only for use with a fixed number of iterations, but your code has no inherent limits on the number of lines it reads from a file.
In theory you could be running MATLAB on Linux or MacOS with a ZFS file system, which permits invididual files as large as 2^64 - 1 bytes. If you transform the while into a for you would have to loop
for LINE = 1:inf
but MATLAB constrains the number of iterations for a for loop to be at most flintmax('double') -- 2^54 . So you would have to use nested loops to get the rest of the potential iterations before you ran into the potential file size limit.

PIERRE OLIVIER SCHOUEL ATANGANA
Hello,
please how to parallelize matlab code which has a for included in a break. eg:
maxLines = 9999999
for k = 1 : maxLines
tline = fgetl(fid);
if ~ischar(textLine)
% End of file so break out of loop.
break;
end
%there are all instructions below
end
  댓글 수: 13
PIERRE OLIVIER SCHOUEL ATANGANA
PIERRE OLIVIER SCHOUEL ATANGANA 2021년 9월 11일
Please never another worry too,
How do I ignore I / O instructions (write, read, write to file, and read to file) in a MATLAB program?
Walter Roberson
Walter Roberson 2021년 9월 11일
What do you want to have happen in place of the i/o operations?

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


PIERRE OLIVIER SCHOUEL ATANGANA
PIERRE OLIVIER SCHOUEL ATANGANA 2021년 9월 11일
In fact the context is that of the calculation of the ellipsoid from the coordinates of each ball. the block that I want to parallelize is the one that grouping balls from the coordinates.
When I change the for to parfor these 02 instructions cause me problems
  댓글 수: 16
PIERRE OLIVIER SCHOUEL ATANGANA
PIERRE OLIVIER SCHOUEL ATANGANA 2021년 10월 28일
편집: Walter Roberson 2021년 10월 28일
Good evening,
Echan = [Echan2; Echan];
It is a matrix which contains the coordinates of each ball. And a ball is defined by three coordinates x, y and z
Walter Roberson
Walter Roberson 2021년 10월 28일
You do not use the information in Echan to draw the balls.
The information you accumulate in Echan has different coordinates than what you draw with (unless by coincidence.)
The reason I bother mentioning this is that if you are not going to use Echan to draw the balls, then there is no point in building that information and you can throw away a number of lines. Doing that would remove any need for us to worry about what the sizes of various arrays are.
But if you need to use those coordinates to draw the balls, and you want to vectorize it all, then we have to know size() of each variable involved. For example is size(phi) guaranteed to be the same as size(theta) ? Is phi a column vector and theta is a row vector? Are they scalars ?

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

카테고리

Help CenterFile Exchange에서 Model Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by