Please help me make my code more efficient

4 vues (au cours des 30 derniers jours)
Corwin
Corwin le 7 Mai 2024
Commenté : Rik le 7 Mai 2024
I'm working on a signal processing assignment (course information below) in which I need to take a vector which has been permuted ABCD-->CBDA where the letters each represent (in order) one fourth of the components of the vector. I've successfully completed the assignment but would appreciate some critique on the code below. Specifically, I'd like to know how this can be done more efficiently and which operations are memory/time intensive.
What I did was to :
1. Reshape the vector into 4 equal columns of a matrix,
2. transpose that matrix,
3. perform 2 row swaps,
4. transpose back,
5. reshape back to a column vector.
'AudFileFreq' is a vector of length 1,275,264 and 318,816 is 1/4th of 1,275,264.
AudFileFreq=reshape(AudFileFreq,318816,4);
AudFileFreq=AudFileFreq.';
AudFileFreq([3 4],:)=AudFileFreq([4 3],:);
AudFileFreq([1 3],:)=AudFileFreq([3 1],:);
AudFileFreq=AudFileFreq.';
AudFileFreq=reshape(AudFileFreq,318816*4,1);
The course is "The Fourier Transform and Its Applications" taught by Brad Osgood in 2007 and course materials made available by the Stanford Center for Professional Development: https://see.stanford.edu/Course/EE261.

Réponse acceptée

Rik
Rik le 7 Mai 2024

I don't see any way to fundamentally change how your code works to speed it up, other than performing the row swaps in one call:

AudFileFreq=AudFileFreq([4 2 1 3],:);

When you're working with a lot of data, things sometimes just need time.

  4 commentaires
Corwin
Corwin le 7 Mai 2024
Thanks, Dyuman. The reference on Row-Major layout was particularly helpful.
Impressive that you got all that down to one line.
Rik
Rik le 7 Mai 2024
You can also automatically calculate the other elements:
AudFileFreq=reshape(AudFileFreq,[],4);
AudFileFreq=reshape(AudFileFreq(:,[4 2 1 3]),[],1);
The [] in reshape tells Matlab to derive the size from the other dimensions.
I don't see a way to further compress this code while keeping the efficiency.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by