Extracting multiple subarrays from a given array in a loop, when the corresponding mulitple start- and end-point pairs are given

조회 수: 6 (최근 30일)
Assume I have an array of given size, say 100. I have 5 pairs which specify the start and end-point indices of the subarrays I would like to extract from the original array.
How to do this task in a for loop and in each iteration, store the subarray in a different vector? (because all subarrays may not be of same size)

채택된 답변

Daniel
Daniel 2023년 9월 12일
You can create a cell array with curly braces:
cellArray = {};
Elements of a cell array can be anything, for instance variable-size vectors. Even the data types can be different, though that's not relevant to your question.
cellArray{1} = [1 2 3];
cellArray{2} = [single(4)];
cellArray{3} = false;
cellArray{4} = dsp.SineWave;
You can read the cell array the same way as writing it:
for i=1:length(cellArray)
cellArray{i}
end
ans = 1×3
1 2 3
ans = single 4
ans = logical
0
ans =
dsp.SineWave with properties: Amplitude: 1 Frequency: 100 PhaseOffset: 0 ComplexOutput: false Method: 'Trigonometric function' SamplesPerFrame: 1 SampleRate: 1000 OutputDataType: 'double'
Now if you store your starting and ending indices in an appropriate matrix (say 5x2), you can create your cell array of variable-size vectors iteratively in a for loop.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by