transform the while loop into a loop for
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (3)
Walter Roberson
el 1 de Sept. de 2021
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
el 1 de Sept. de 2021
0 votos
13 comentarios
Image Analyst
el 1 de Sept. de 2021
Do you even have the Parallel Computing Toolbox? Type ver to find out. But even if you do, I'm not sure it can be parallelized. But I haven't played around with parallelizing code much.
If you want, you can use fileread() to suck up the whole file into one character variable.
Walter Roberson
el 1 de Sept. de 2021
What would be run in parallel? Reading different files? Reading different lines from the same file?
PIERRE OLIVIER SCHOUEL ATANGANA
el 6 de Sept. de 2021
Walter Roberson
el 6 de Sept. de 2021
If you want to run multiple files in parallel, then you could
parfor fileIdx = 1 : number_of_files
thisfile = filenames{fileIdx};
fid = fopen(thisfile);
... do the reading and mathematical processing
fclose(fid)
results{fileIdx} = {Pts, Ray, Vol};
end
However if you are doing this, there is no reason to swtich from while loop to for loop to read the lines.
If the files fit into memory, there are notably more efficient ways of processing the input that do not use for or while to read the values for any particular file.
Image Analyst
el 6 de Sept. de 2021
Pierre, you never answered my question and said whether you in fact HAVE the Parallel Processing Toolbox. Type ver and show us what it says (except for your license number of course).
PIERRE OLIVIER SCHOUEL ATANGANA
el 6 de Sept. de 2021
Editada: Image Analyst
el 6 de Sept. de 2021
Image Analyst
el 6 de Sept. de 2021
OK, you have an enormous number of toolboxes including the Parallel Computing Toolbox so you should be able to use parallel processing. Have you gone through the demos/examples provided with that toolbox?
PIERRE OLIVIER SCHOUEL ATANGANA
el 8 de Sept. de 2021
Image Analyst
el 8 de Sept. de 2021
Yes. Look at this page:
Especially look at the Getting Started link and the other links on the left hand side of the page.
PIERRE OLIVIER SCHOUEL ATANGANA
el 11 de Sept. de 2021
Walter Roberson
el 11 de Sept. de 2021
Those two lines do not appear in what you have posted so far, so we do not have the context for them. We do not know what members is, and we do not know this is after the parfor where you would be working with the accumulated results -- we can't be sure that Pts and Ray are numeric arrays by then because we do not have the context.
PIERRE OLIVIER SCHOUEL ATANGANA
el 11 de Sept. de 2021
Walter Roberson
el 11 de Sept. de 2021
What do you want to have happen in place of the i/o operations?
PIERRE OLIVIER SCHOUEL ATANGANA
el 11 de Sept. de 2021
0 votos
16 comentarios
Walter Roberson
el 11 de Sept. de 2021
Editada: Walter Roberson
el 16 de Sept. de 2021
Which two instructions?
I get the impression that you did not use the strategy I showed in
PIERRE OLIVIER SCHOUEL ATANGANA
el 16 de Sept. de 2021
PIERRE OLIVIER SCHOUEL ATANGANA
el 16 de Sept. de 2021
Walter Roberson
el 16 de Sept. de 2021
Pts and Ray are not broadcast variables in the outline I posted.
You are asking us to debug code that we cannot see.
Walter Roberson
el 16 de Sept. de 2021
Editada: Walter Roberson
el 16 de Sept. de 2021
You never mex a .m file. You mex code that is written in other languages, such as
mex my_code.c
mex is not the command to compile .m code into binary; that would be mcc https://www.mathworks.com/help/compiler/mcc.html
If you were to generate code from a .m file, then you would attach the generated object, such as a .mexw64 file, not the .m file.
PIERRE OLIVIER SCHOUEL ATANGANA
el 16 de Sept. de 2021
PIERRE OLIVIER SCHOUEL ATANGANA
el 16 de Sept. de 2021
PIERRE OLIVIER SCHOUEL ATANGANA
el 17 de Sept. de 2021
Walter Roberson
el 17 de Sept. de 2021
1.The entire array or structure 'Pts' and Ray is a broadcast variable.This might result in unncessary communication overhead
That is not an error, that is a warning.
In the context of the code you posted at https://www.mathworks.com/matlabcentral/answers/1444364-transform-the-while-loop-into-a-loop-for#comment_1739794 then the way you deal with that warning is to rewrite the loop as
parfor i=1:k
disp(strcat('Cluster numero <',num2str(i),'>'));
disp('De Centroid');
disp(C(i,:));
end
and leave out the assignment to souEns and sousRay . Those assignments you are doing are assignments to local variables that will be discarded after the parfor, so there is no point in computing them. The only productive work your parfor is doing is displaying the cluster numbers and associated centroid.
Walter Roberson
el 17 de Sept. de 2021
Editada: Walter Roberson
el 17 de Sept. de 2021
Pt = V1 * poin;
What size is V1 ? It looks to me that it is most likely a 3 x 3 matrix. If so then,
%do this part ONCE
syms X1 Y1 Z1
syms v1 [3 3]
syms Centro [3 1]
poin = [X1, Y1, Z1].';
pt = v1 * poin + Centro(:)
ptfun = matlabFunction(pt, 'vars', {v1, Centro, X1, Y1, Z1})
%now each iteration with new V1, centro, x1, y1, z1, do the following
Pt = Ptfun(V1, centro(:), permute(x1, [3 1 2]), permute(y1, [3 1 2]), permute(z1, [3 1 2]));
x1 = permute(Pt(1,:,:), [2 3 1]);
y1 = permute(Pt(2,:,:), [2 3 1]);
z1 = permute(Pt(3,:,:), [2 3 1]);
Walter Roberson
el 17 de Sept. de 2021
Editada: Walter Roberson
el 17 de Sept. de 2021
Or... you could do
TM = [V1, centro(:)];
XYZ1 = [x1(:), y1(:), z1(:), ones(numel(x1),1)];
pt = TM * XYZ1;
x1 = reshape(pt(1,:), size(x1));
y1 = reshape(pt(2,:), size(y1));
z1 = reshape(pt(3,:), size(z1));
PIERRE OLIVIER SCHOUEL ATANGANA
el 20 de Sept. de 2021
PIERRE OLIVIER SCHOUEL ATANGANA
el 26 de Oct. de 2021
Editada: Walter Roberson
el 26 de Oct. de 2021
Walter Roberson
el 26 de Oct. de 2021
Echan=[Echan2;Echan];
You do not use that information, so it is not clear that it is worth calculating it.
You appear to draw a sphere at each location. Is it correct that you want that merged into a single graphics object? Doing that might be a bit tricky unless you switch to patch()
PIERRE OLIVIER SCHOUEL ATANGANA
el 28 de Oct. de 2021
Editada: Walter Roberson
el 28 de Oct. de 2021
Walter Roberson
el 28 de Oct. de 2021
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 ?
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!