Please help me make my code more efficient

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.

 Risposta accettata

Rik
Rik il 7 Mag 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 Commenti

Thanks, I was also feeling a little uncomfortable with the matrix operations in matlab so seeing how you permuted rows there gives me a little more insight into the syntax.
Am I roughly correct in visualizing matlab matrices as arrays with pointers attached that say "new row begins here." Such that reshape is just a matter of moving a few pointers around rather than needing to allocate new memory for data or move elements in the array?
" "new row begins here." "
Column*. By default, Matrices in MATLAB are stored column-wise. But yes, your understanding is correct.
"Such that reshape is just a matter of moving a few pointers around rather than needing to allocate new memory for data or move elements in the array?"
Yes, that is correct.
Edit -
AudFileFreq=reshape(AudFileFreq,318816,4);
%Here's a single line compact code
AudFileFreq=reshape(AudFileFreq(:,[4 2 1 3]),318816*4,1);
Thanks, Dyuman. The reference on Row-Major layout was particularly helpful.
Impressive that you got all that down to one line.
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.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 7 Mag 2024

Commentato:

Rik
il 7 Mag 2024

Community Treasure Hunt

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

Start Hunting!

Translated by