필터 지우기
필터 지우기

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