Vectorizing a nasted loop

조회 수: 3 (최근 30일)
Oded T
Oded T 2019년 5월 1일
편집: Oded T 2019년 5월 2일
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)
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
Oded T 2019년 5월 2일
yes - i did preallocated the 2 arrays before exactly like you specified

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

채택된 답변

Rik
Rik 2019년 5월 1일
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.
  댓글 수: 4
Oded T
Oded T 2019년 5월 2일
편집: Oded T 2019년 5월 2일
both actually,
i want to understand each row and part and the logic behind it
Guillaume
Guillaume 2019년 5월 2일
There's nothing mysterious about it. It's simply reshaping a vector/matrix in the desired shape (in your case a Window * Chunks array). The idx business is just in case your original matrix/vector is larger than the desired result, and simply crops the original array/matrix to the required number of elements.
If the original array/matrix has already the required number of elements, then you don't need to bother with idx. See my answer.

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

추가 답변 (1개)

Guillaume
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
Guillaume
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?
Oded T
Oded T 2019년 5월 2일
편집: Oded T 2019년 5월 2일
hi,
here is some details:
  • the RAW data is a signal of sound from Microphone
  • I need to seperate it into chunks - because i want to prepare these chunks in FFT, and analysing the relevant chunks (windowing). i.e. take a part from time 13.5sec up to 14sec - FFT it and analysing it.
description about what i try to implement:
  1. take a full signal from the microphopne
  2. seperate it into X chunks determined by the time - i.e. chunks of 0.5sec each for examples.
  3. FFT these chunks accordingly and save it in a matrix of chunks by time domain and by F domain.
  4. than i can choose the part of the signal i want to analysis - and plot it by time domain and F domain.
i have another issue with the FFt, but first i want you to understand better athe context.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by