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 Comments

Did you preallocate the two output arrays? That should speed things up considerably.
Add this before the loop:
Datachunk = zeros(Window, Chunk)
yes - i did preallocated the 2 arrays before exactly like you specified

Sign in to comment.

 Accepted 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.

4 Comments

Oded T
Oded T on 2 May 2019
Edited: Oded T on 2 May 2019
works like a magic :)
0.1 seconds instead of 49 (!!)
thank you very much, can you please try and explain how it works?
i really want to understand how to do it, but this code looks pretty unclear for me
Which part do you want me to explain? The indexing, or the way I created the indexing array?
Oded T
Oded T on 2 May 2019
Edited: Oded T on 2 May 2019
both actually,
i want to understand each row and part and the logic behind it
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.

Sign in to comment.

More Answers (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 Comments

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).
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.
Oded T
Oded T on 2 May 2019
Edited: Oded T on 2 May 2019
hi Rik and Guillaume,
so my next question relevant for both of you, becuase i find it hard to understand if I'm doing it right:
i changed my code that way - that instead of specify the size of the data chunks by number of samples - i will do that by specify the length of the time i want, so the calculation to get the desired number of samples is pretty simple:
Nsamples = T(time_Length)*Fs(sample_rate).
after implementing it - i won't get all the time integers, sometime i will get some fractions in the Nsamples.
so i using a "floor" function in order to round it down (so i will not get any matrix dimension errors), for example:
by choosing 0.5sec, with data rate of 49,000sample/sec i get - 124.0816 chunk size, floor round it to 124.
now i get a little lost - what solution should i choose? rik's or Guillaume's ?
am i missing something else by rounding down the data-chunks ?
my assumption is that i will loose only the last part of data, but maybe i'm doing some additional damage?
any help will be appriciated
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 on 2 May 2019
Edited: Oded T on 2 May 2019
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.

Sign in to comment.

Categories

Asked:

on 1 May 2019

Edited:

on 2 May 2019

Community Treasure Hunt

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

Start Hunting!