How do I vectorise a for loop that reorders 4 bytes of an array with each iteration

조회 수: 1 (최근 30일)
A = uint8(Data); %Assume the data comes in multiples of 4 bytes
B = [];
for byteNo = 1 :4: length(A)-3 % changed 4 to 3 to fix the typo that was flagged by Walter Roberson
B = cat(2,B,[A(byteNo+2),A(byteNo+3),A(byteNo),A(byteNo+1)]);
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 9월 7일
I suspect the termination should be length(A)-3 rather than -4 . Consider that if A was length 8, then there would be room for two swaps of length 4, but your upper bound would be byteNo = 4 which is before the second lower byteNo needed for the swap, which has to start at byteNo = 5.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 9월 7일
If the vector is not a multiple of length 4, discard mod(length,4) values from the end of it. Take the resulting vector and reshape() it into 4 rows. Reorder the rows. Reshape the result into a vector.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by