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

2 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by