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!