Vectorizing a nasted loop
古いコメントを表示
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 ?
2 件のコメント
Jos (10584)
2019 年 5 月 1 日
Did you preallocate the two output arrays? That should speed things up considerably.
Add this before the loop:
Datachunk = zeros(Window, Chunk)
Oded T
2019 年 5 月 2 日
採用された回答
その他の回答 (1 件)
Guillaume
2019 年 5 月 1 日
DataChunk = reshape(Raw.RAW, Window, Chunks);
TimeChunk = reshape(Raw.Time, Window, Chunks);
That's assuming that RAW and Time are structure fields or object properties and not object methods.
5 件のコメント
Rik
2019 年 5 月 1 日
It also assumes the size of both is Window*Chunks (and my solution assumes they are at least that size, but so does the original code).
Guillaume
2019 年 5 月 2 日
Yes, of course. There is a lot of information missing in the question.
To Oded, if numel(Raw.RAW) > Window*Chunks, you should use Rik's solution. If numel(Raw.RAW) == Window*Chunks, you should use mine as it's simpler in that case.
Guillaume
2019 年 5 月 2 日
I think we need a bit more context to understand your question. It seems you want to divide some sampled data into chunks. Why? What are you going to do with it?
Reshaping the samples into a matrix has some advantages as matrix are easier to work with, but it forces you to have the same number of samples in each chunk, which by the sound of it may be a problem. What are you doing with the matrix afterward?
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!