I have this code, where 'data' changes its size with every loop and has to be divided into four equal segments. This division has to be stored in variable 'epoch'. storing it in cell variable isn't an option. How can I store differen lengths of data?

조회 수: 1 (최근 30일)
data = y;
seg = y/4;
signal = data(1,:);
for e = 1:4
epoch (e,:) = signal(1+(e-1)*seg:e*seg);
end

채택된 답변

Guillaume
Guillaume 2018년 12월 4일
epoch = reshape(signal, [], 4).'
No need for a loop.
  댓글 수: 6
Bubblesjinx
Bubblesjinx 2018년 12월 4일
Thanks for stating all the possible options. For my requirement, I would go for "splitting into four equal parts using a cell array", for which I have tried following:
epochs{1} = e;
epochs{2} = signal(1+(e-1)*seg:e*seg);
Then I have following error:
Unable to perform assignment because brace indexing is not supported for variables of this type.
Guillaume
Guillaume 2018년 12월 4일
You get this error because you're reusing epoch that you previously created as a double array. You would have avoided this problem if you had preallocated epoch before the loop. It's always a good to preallocate arrays that you're assigning to in a loop. It makes the loop faster, and avoids this type of errors:
epoch = cell(1, 4); %preallocate epoch
for .... your loop as normal
As usual for matlab, there's no need for the loop (and the preallocation):
defaultlength = ceil(numel(signal) / 4);
epoch = mat2cell(signal, 1, [ones(1, 3) * defaultlength, defaultlength + mod(defaultlength, -4)])
The last element of epoch will be up to 3 elements shorter than the previous 3.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by