# Is there a better way to solve this multidimensional array?

6 visualizaciones (últimos 30 días)
Bianka Markovic el 11 de Jun. de 2021
Comentada: Bianka Markovic el 14 de Jun. de 2021
Hello to everyone,
I'm currently trying to calculate annual temperatures and temperature anomalies. The data is given in one variable (here the M variable). It entails the temperatures for different longitude and latitude (basically temperatures on a regular grid), different depth and time. The first entry is the longitude and the second the latitude. The third one is for the different depth and the forth the time. The time represents 24 years, 12 entries per year meaning one temperature value for each month of the year.
The first loop is working fine even thou it takes some time. But the second loop takes too much time and effort that it never got through before my laptop broke down.
Does anyone knows maybe why it is not working, or has anyone a suggestion how could I make the program more efficient?
Thank you!
M=rand(1920,374,6,288);
sizetemp=size(M);
temp_mean=[];
for i=1:sizetemp(1); %longitude
for j=1:sizetemp(2); %latitude
for k=1:sizetemp(3) %depth
temp_mean(i,j,k)=mean(M(i,j,k,:));
end
end
end
m=1;
temp_annual=[];
an_temp=[];
for i=1:sizetemp(1); %longitude
for j=1:sizetemp(2); %latitude
for k=1:sizetemp(3) %depth
for l=1:12:sizetemp(4);
temp_annual(i,j,k,m)=mean(M(i,j,k,l:l+11));
an_temp(i,j,k,m)=temp_mean(i,j,k)-temp_annual(i,j,k,m);
m=m+1;
end
end
end
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

SALAH ALRABEEI el 11 de Jun. de 2021
For the first averaging
%
temp_mean = nanmean(M,4);
For the 2nd annual avg use
%
A=reshape(M,19,3,6,size(M,4)/12,12);
an_temp = nanmean(A,5);
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Bianka Markovic el 14 de Jun. de 2021
unfortunately the program shows an error:
Error using reshape
To RESHAPE the number of elements must not change.
Error in test (line 44)
A=reshape(M,19,3,6,size(M,4)/12,12 );

Iniciar sesión para comentar.

### Más respuestas (1)

Sulaymon Eshkabilov el 11 de Jun. de 2021
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Bianka Markovic el 14 de Jun. de 2021
Its still taking too long to process :(

Iniciar sesión para comentar.

### Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by