Stuck in endless loop

Hi all,
I am learning matlab thru practice and patience. I am trying to create a loop to get the mean of a precipitation dataset with a 0.5 grid spacing and dimensions x=141, y=71 and t =38. I am trying to build a structure to get the means per grid over each layer (38). I am running the following code but matlab is stuck in a endless loop. Is there any way to fix this to avoid getting stuck? Thanks in advance for your help
for t=1:38
for x=1:141
for y=1:71
meanprecip(y,x,t)=nanmean(rain(y,x,t))
end
end
end

6 Commenti

Emmanuel - the code is probably not getting stuck in an endless loop since your for loops are well defined. The problem could be that the code is just slow. Try pre-sizing your meanprecip array since you know the dimensions of it:
meanprecip = zeros(38,141,71);
for t=1:38
% etc.
end
You may be able to optimize your code and remove some one (or more?) loops. I haven't downloaded your rain data, but what does rain(y,x,t) return? Is it a scalar or an array? If a scalar, what does nanmean do for this single value?
desert_scientist90
desert_scientist90 il 24 Ott 2019
This data is supossed to display a map when using Pcolor. Since is satellite data nan represents points over the ocean. Nanmean is to ignore the nan in the dataset.
Geoff Hayes
Geoff Hayes il 24 Ott 2019
So is rain(y,x,t) a scalar? I think it might be and so your first layer is rain(:,:,1), a 71x141 2D array. Do you really want to calculate the nanmean of this 2D array? Or a row or column?
desert_scientist90
desert_scientist90 il 24 Ott 2019
Yes is a scalar, I am trying to get the means over the years exm mean of 1979, 1980,1981..ect. to calculate the z-scores and then identify low and high values. To create a new map with the differences.
Geoff Hayes
Geoff Hayes il 24 Ott 2019
So if it is a scalar, then you are not calculating the mean over a set of years...just a single value. Which dimension represents years?
desert_scientist90
desert_scientist90 il 24 Ott 2019
t=38 represents the years.

Accedi per commentare.

Risposte (1)

Siriniharika Katukam
Siriniharika Katukam il 29 Ott 2019
Modificato: Siriniharika Katukam il 29 Ott 2019
Hi,
As per my understanding, you are trying to find the mean of grid at each layer. In your case, if the output of rain(y,x,t) is a scalar, then calculating mean of a scalar is like reassigning. It is similar to
meanprecip(y,x,t) = rain(y,x,t);
If you want to calculate the mean of the grid at each year,
Try this:
for t=1:38
meanprecip(t) = mean(rain(:,:,t))
end
Moreover, your code is not an endless loop.

Community Treasure Hunt

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

Start Hunting!

Translated by