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
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?
Stephanie Diaz
Stephanie Diaz 2017-6-5
Hi @Walter, the breakpoint will always be a value of 1. I can never have "[3423]" because the numbers will always start at 1 and ascend, but can end at any value.

请先登录,再进行评论。

采纳的回答

Guillaume
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
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.
Stephanie Diaz
Stephanie Diaz 2017-6-5
@Guillaume got it, thanks!

请先登录,再进行评论。

更多回答(1 个)

Phil
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
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 CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by