Can anyone help me with the a tentative guide on how to average across the two dimensions of a 3d array, and loop it across 86 timesteps?
Mostrar comentarios más antiguos
Its a climate data of type 720x360x86. Thank you.
Respuesta aceptada
Más respuestas (1)
Let's do this with a smaller array, as the specific numbers don't seem particularly important. I'll do 5x4x3 so we can see the values easily. It's actually very easy because MATLAB works naturally with matrices of any shape, you can do this all with the mean function, no loops required:
X = rand(5,4,3)
a=mean(X,1) % mean across rows (same as mean(X))
b=mean(X,2) % mean across columns
You might find the shape of these outputs to be annoying, the squeeze function is good for fixing that up:
squeeze(a)
squeeze(b)
12 comentarios
Rohit shaw
el 11 de Oct. de 2021
This makes more sense, sorry about the confusion.
In recent versions of MATLAB you can use the vecdim argument to mean which allows you to specify multiple dimensions across which to take the mean (the same strategy with squeeze applies as above):
a=rand(5,4,3)
b = mean(a,[1 2])
c = squeeze(b)
I'm not sure what release vecdim was introduced, if you're on a release where specifying a vector of dimensions for the mean is not supported, you can use reshape to acieve the same effect:
d = reshape(a,5*4,3) % in your case reshape(themat,720*360,86) or generally reshape(themat,height(themat)*width(themat),size(themat,3))
e = mean(d)
Rohit shaw
el 11 de Oct. de 2021
Dave B
el 11 de Oct. de 2021
@Rohit shaw - Does this solve the issue? I should have said the short version is:
squeeze(mean(yourdata,[1 2]))
Rohit shaw
el 11 de Oct. de 2021
vecdim should be taking positive integers, namely 1 and 2, as those are the dimensions across which you're taking a mean...right?
Can you clarify what you mean by isn't working correctly? I maye still be misunderstanding your problem. I read it as: take the mean for each 'slice' in 3-D matrix...
a = cat(3,[1 2;3 4],[5 6; 7 8])
mean_1 = squeeze(mean(a,[1 2]))'
mean_2 = mean(reshape(a,4,2))
mean_3 = [mean([1 2 3 4]) mean([5 6 7 8])]
Rohit shaw
el 11 de Oct. de 2021
Dave B
el 11 de Oct. de 2021
@Rohit shaw vecdim should literally be the numbers 1 and 2, they are both positive and integer. They have to be positive integers because you index into matrices with positive integers in MATLAB.
If the plot is not expected, is it possible that the data in the matrix are not what you think they are? You could check mean2(thematrix(:,:,1)) or mean2(thematrix(:,:,2)) and compare that to the first or second item in your result.
I'm pretty confident that this method works, both because I've used it often, and because of the example code above which is unambiguously correct.
Rohit shaw
el 11 de Oct. de 2021
Dave B
el 11 de Oct. de 2021
Hi Rohit - sorry again about the confusion.
The reason you're getting that error is because you're on an older version of MATLAB and vecdim isn't an available option yet. (As I mentioned above I'm not sure what release vecdim was introduced), the bit that's going wrong is not the positive/integer part but the fact that it's not scalar, as before vecdim was an option this argument was always a scalar. You can see why we added this option, it's a common question and the reshape approach is confusing!
Hope you can get to the bottom of it and I look forward to finding out what was going on.
Rohit shaw
el 11 de Oct. de 2021
Rohit shaw
el 17 de Oct. de 2021
Categorías
Más información sobre Climate Science and Analysis 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!
