Please help me make my code more efficient

조회 수: 3 (최근 30일)
Corwin
Corwin 2024년 5월 7일
댓글: Rik 2024년 5월 7일
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