MATLAB: Vectorizing a nasted loop

nasted loopvectorizing

hello,
I'm using a simple nasted loop for assignment:
for i=1:Chunks %loop for number of chunks
for n=1:Window
DataChunk(n,i) = Raw.RAW(idx); %for each itteration - take the right raw data into the right chunk
TimeChunk(n,i) = Raw.Time(idx);
idx = idx+1; % increment the indes
end
end
this loop takes forever for a very big amount of data,
anyome can please help me vectorize it ?

Best Answer

This should work:
idx=reshape(1:(Chunks*Window),Window,Chunks);
DataChunk=Raw.RAW(idx);
TimeChunk=Raw.Time(idx);
The output should be the correct size, as it usually retains the shape of the index. If this is not the case, you can always use reshape to adjust the arrays to suit your needs.
If idx didn't start at 1 in your code, you can add the old value after the reshape.