我有一个执行以下功能的for循环:
取一个 M x 8 矩阵和:
- 将其拆分为大小为 512 个元素的块(表示 X 乘以 8 的矩阵 == 512,元素个数可以是 128,256,512,1024,2048)
- 将块重塑为 1 x 512(元素数)矩阵。
- 取矩阵的最后 1/4 放在前面,
例如Data = [Data(1,385:512),Data(1,1:384)];
以下是我的代码:
for i = 1 : NumOfBlock
if i == 1
Header = tempHeader(1:RowNeeded,:);
Header = reshape(Header,1,BlockSize); %BS
Header = [Header(1,385:512),Header(1,1:384)]; %CP
Data = tempData(1:RowNeeded,:);
Data = reshape(Data,1,BlockSize); %BS
Data = [Data(1,385:512),Data(1,1:384)]; %CP
start = RowNeeded + 1;
end1 = RowNeeded * 2;
else
temp = tempData(start:end1,:);
temp = reshape(temp,1,BlockSize); %BS
temp = [temp(1,385:512),temp(1,1:384)]; %CP
Data = [Data, temp];
end
if i <= 127 & i > 1
temp = tempHeader(start:end1,:);
temp = reshape(temp,1,BlockSize); %BS
temp = [temp(1,385:512),temp(1,1:384)]; %CP
Header = [Header, temp];
end
start = end1 + 1;
end1=end1 + RowNeeded;
end
使用 500 万个元素运行此循环将需要 1 个多小时。我需要它尽可能快(以秒为单位)。这个循环可以被矢量化吗?