Please help me make my code more efficient

3 次查看(过去 30 天)
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.

采纳的回答

Rik
Rik 2024-5-7

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 个评论
Corwin
Corwin 2024-5-7
Thanks, Dyuman. The reference on Row-Major layout was particularly helpful.
Impressive that you got all that down to one line.
Rik
Rik 2024-5-7
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by