필터 지우기
필터 지우기

How can I split an array into equal overlapping sub-arrays?

조회 수: 29 (최근 30일)
mark palmer
mark palmer 2020년 4월 9일
댓글: mark palmer 2020년 4월 10일
How can I split an array into equal overlapping sub-arrays?
Simple example where the array divides evenly without any need for overlapping
ArrayX = 1:160;
ArraySplitSize = 4
Result is
1:40, 40:80, 80:120, 120:160
Example where overlapping is needed. I want to keep full size of the array and not cut off anything at the end.
ArrayX = 1:100;
Subarray = 40;
ArraySplitSize = 3;
Result is 1:40, 30:70, 60:100
  댓글 수: 3
Kelly Kearney
Kelly Kearney 2020년 4월 10일
Also, what's the rule you used to decide which of the overlapping blocks get shorted when the array isn't evenly divisible? E.g. in your latter example, you indicate subarrays of length 40, 41, and 41. Also, in your first example, you listed values with overlaps; I assume that should be 1:40, 41:80, 81:120, and 121:160, right?
mark palmer
mark palmer 2020년 4월 10일
Right.
The subarrays don't have to be exactly equal-divided across the main array, as closly as possible is fine, but they all should be the same size (in this case 40).
It's not homework at all, I'm just not a pro programmer, am doing this for an art project.

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 10일
If you have the Communications Toolbox, the easiest approach is to use buffer() and take the columns of the resulting matrix. buffer() already has logic for overlapping windows built in. Otherwise,
bufflen = length(buffer);
advance = window_size - overlap;
starts = 1 : advance : bufflen;
ends = min(starts + window_size - 1, bufflen);
result = arrayfun(@(S,E) buffer(S:E), starts, ends, 'uniform', 0);
This uses a strictly uniform size. The last few entries might be shorter.
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 4월 10일
bufflen = length(ArrayX);
starts = floor(linspace(1, bufflen-Subarray+1, ArraySplitSize));
ends = starts + Subarray - 1;
result = arrayfun(@(S,E) ArrayX(S:E), starts, ends, 'uniform', 0);
mark palmer
mark palmer 2020년 4월 10일
Works! Thank you so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Hamming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by