parfor nested for loops
Mostrar comentarios más antiguos
The following script works finely in for loop, but I need to implement parfor to speed up. I am looking for help how to implement parfor in the following nested for loop. Many thanks for your kind help.
Z_anom = zeros(n,m);
d = 0;
for y = 1:n
for c_day = 1:m
d = d + 1;
g = 0; % counter for the grid point
for i = 1:nlat
for j = 1:nlon
g = g + 1;
Z_anom(d,g) = (Z(c_day,y,i,j) - Z5(c_day,i,j))*sqrt(cosd(datalat(i)));
end
end
end
end
6 comentarios
OCDER
el 21 de Ag. de 2018
What are the values of these?
n
m
nlat
nlon
What are the sizes of these?
Z
Z5
datalat
Before using parfor, try to vectorize the Z-Z5*sqrt(costd(...)). It'll be much faster.
Eb Bed
el 21 de Ag. de 2018
Walter Roberson
el 21 de Ag. de 2018
Well, for one thing
sqrt(cosd(datalat(i)))
only needs to be calculated once each time i changes.
OCDER
el 21 de Ag. de 2018
Can you double check this math?
(Z(c_day,y,i,j) - Z5(c_day,i,j))*sqrt(cosd(datalat(i)));
^ are you missing "y,"?
Eb Bed
el 21 de Ag. de 2018
OCDER
el 22 de Ag. de 2018
Hm, something isn't adding up.
Z_anom = zero(n, m);
for y = 1:n
for c_day = 1:m
d = d+1;
...
Z_anom(d, g) = ...
end
end
This means size of Z_anom will [n*m, nlat*nlon], and NOT [m, n] as you started out with.
Also, datalat size is [80, 80], but it seems you are accessing i from 1:nlat, where nlat = 80?
Please provide the size of all variable as per my 1st comment.
Respuestas (0)
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!