Is there a way to optimise following loop using 1:end notation?

1 view (last 30 days)
for ii = 1:NumberElements
for jj = 1:NumberElements
E(:,ii,jj) = fourier(A(:,ii,jj));
E1(:,ii,jj) = fft(A(:,ii,jj));
end
end

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 28 Aug 2020
You should get an identical fft in one step if you do this:
E1mat = fft(A,[],1);
Here the third argument is the dimension of A to Fourier-transform along. As for the first expression the only fourier I find in my matlab-installation is in the symbolic toolbox, and that cannot be the one you're calling above. One thing you definitely should do is pre-allocate E - when you loop from 1 and assign values to a new row or column matlab needs to reallocate memory-space every step in the loop. If you cannot figure out how big the array will be but can safely run the loops in opposite direction that is a lazy-man's alternative:
for ii = NumberElements:-1:1
for jj = NumberElements:-1:1
E(:,ii,jj) = fourier(A(:,ii,jj));
end
end
That way you make space for thefull array first time around the loops.
HTH

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!