Info
This question is closed. Reopen it to edit or answer.
How can I make my code faster?
1 view (last 30 days)
Show older comments
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 Comments
Rik
on 10 Apr 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
on 10 Apr 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?
Answers (0)
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!