How can I split a vector?
조회 수: 73 (최근 30일)
이전 댓글 표시
Hi,
I have a vector that looks something like:
v=[1,2,3,4,1,2,3,4,1,2,3]
I need to separate this vector into chunks of 1-n, with n being less than 1. So in the above example, this vector would be separated into the following:
[1,2,3,4] [1,2,3,4] [1,2,3]
I then have to count the number of elements in each of these, like:
[4] [4] [3]
These values can then be stored in another vector. How can I achieve this?
댓글 수: 2
Walter Roberson
2017년 6월 2일
If you had
[ 3 4 2 3]
then how would you know if the breakpoints should be
[3 4] [2 3]
or
[3 4], [2], [3]
since the 2 might be "started a new cycle that happens to contain only one element" followed by "started a new cycle that happens to contain only one element" ?
Is it the case that a value followed by a smaller value always signals the end of a cycle, but that a value followed by a greater value never signals the end of a cycle? Is it as simple, in other words, as looking for places where a value is followed by a smaller value?
채택된 답변
Guillaume
2017년 6월 3일
As you haven't answered Walter's answer it's difficult to know what you want exactly. Assuming you want to split your vector in monotonically increasing sequences, then:
v=[1,2,3,4,1,2,3,4,1,2,3]
seqlengths = diff([0, find(diff(v) < 0), numel(v)])
sequences = mat2cell(v, 1, seqlengths)
Note: the same algorithm also works for Phil's answer which assumes that a sequence starts at 1.
seqlengths = diff([find(v == 1) - 1, numel(v)]) %assumes that first sequence starts with 1
sequences = mat2cell(v, 1, seqlengths)
댓글 수: 4
Guillaume
2017년 6월 5일
It basically finds where the transition between the sequences occur. In the first case, it's when the difference between two consecutive numbers is negative, in the second case it's just before a 1. The difference between the indices of these transitions is thus the length of each sequence.
추가 답변 (1개)
Phil
2017년 6월 3일
I think the following function will do what you want i.e. take a vector and split it wherever a 1 occurs:
function splitVectors = getSplitVectors(originalVector)
splitIndexes = find(originalVector==1);
numSplits = numel(splitIndexes);
splitVectors = cell(numSplits, 1);
for i=1:numSplits-1
splitVectors{i} = originalVector(splitIndexes(i):splitIndexes(i+1)-1);
end
splitVectors{numSplits} = originalVector(splitIndexes(numSplits):end);
end
example usage:
>> v = [1 2 3 4 1 2 3 4 1 2 3];
>> splits = getSplitVectors(v);
'splits' is a cell array, you can access each of the vectors with curly braces as follows:
>> splits{1}
ans =
1 2 3 4
>> splits{2}
ans =
1 2 3 4
>> splits{3}
ans =
1 2 3
To get the counts of elements you could then do something like:
>> counts = zeros(numel(splits), 1)
>> for i=1:numel(counts)
counts(i) = numel(splits{i})
end
댓글 수: 2
Phil
2017년 6월 3일
Of course, if all you require is the counts and you don't need to keep the split vectors, then all the information you need to do that is in 'splitIndexes'; you just need to take the difference between neighbouring elements. Pay special attention to the last count though.
참고 항목
카테고리
Help Center 및 File Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!