Info

此问题已关闭。 请重新打开它进行编辑或回答。

How can I make my code faster?

1 次查看(过去 30 天)
Ali Almakhmari
Ali Almakhmari 2023-4-9
关闭: Ali Almakhmari 2023-4-10
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 个评论
Rik
Rik 2023-4-10
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 2023-4-10
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?

回答(0 个)

此问题已关闭。

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by