Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

How can I make my code faster?

1 visualización (últimos 30 días)
Ali Almakhmari
Ali Almakhmari el 9 de Abr. de 2023
Cerrada: Ali Almakhmari el 10 de Abr. de 2023
Hey guys. So I have this code here
for kk = 1:37
for lag = 1:50000
if(lag == 1)
else
r_C(kk,:,:) = circshift(r_C(kk,:,:),1,2); %shifting the vectors
end
for h = 1:100 %for loop to take the mean of each 500 elements (basically shortning the 50000 elements to 100 elements)
DATA(lag,h) = mean(r_C(kk,h,(((h-1)*500)+1):(h*500)));
end
DATAs(lag,:) = fft(DATA(lag,:));
end
end
r_C is a matrix that I have and is the size of 37 by 100 by 50000. Esstentially what I am doing is shifiting r_C, then dividing r_C into 100 blocks with 500 elements in each block, take the mean of each block, save it in DATA, and then take the fft of it and save it in DATAs. This process takes so much time, and I was wondering if there is anyway to make it run faster.
  5 comentarios
Rik
Rik el 10 de Abr. de 2023
I have a feeling that the h loop can be replaced with a reshape of r_C(kk,:,:) in a temporary variable. Creating that indexing array and performing that indexing looks expensive.
But as Dyuman aluded to, this is unlikely to get fast. The best you can expect is less slow.
Joe Vinciguerra
Joe Vinciguerra el 10 de Abr. de 2023
I might have some ideas that could help you, but to start, are you sure this code does what you expect? ...
You're doing a circular shift 37x49999 times on a 37x100x50000 array in the 2nd dimension, which means it's repeating itself about 18500 times (If my math is correct) unnessessarily.
When you loop through at h=1:100, you're shifting in the 2nd and 3rd dimensions at the same time, so your blocks for calculating the mean are moving along a diagonal.
Is this the intent?

Respuestas (0)

La pregunta está cerrada.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by