Question

Here is my dataset:

pressure(time, case, x, y)

>> size(pressure)
ans =
   100     1    289   570

How to get a spatial nanmean pressure for x from 30 to 60 and y from 40 to 70 in each time step?

For example: a nanmean value for that particular region for each timestep from time 1 to time 100.

I tried this, "spatial_mean_pressure = nanmean(pressure(:,:,30:60,40:70))" It averaged the pressure in the timeserie. This is not the result I want.

>> size(spatial_mean_pressure)
ans =
1     1    31    31

I like to get the results like this:

>> size(spatial_mean_pressure)    
ans =
100     1    1    1
Was it helpful?

Solution

You are trying to get a mean for an entire block of matrix. Therefore, you should apply nanmean twice and not once. Also, apply it along a particular dimension to get the desired result. I think this is what you want.

x=randi(10,[100 1 10 25]);

First take the mean along the third dimension.

mean_x_3=nanmean(x,3);

You would get an answer of size = [100 1 1 25]. Then take the mean along 4th dimension.

mean_x_4=nanmean(mean_x_3,4);

This should give you the desired answer. You can write this in one line as,

mean_x = nanmean(nanmean(x,3),4);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top