How can I split a vector?
73 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Stephanie Diaz
el 2 de Jun. de 2017
Comentada: Stephanie Diaz
el 5 de Jun. de 2017
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 comentarios
Walter Roberson
el 2 de Jun. de 2017
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?
Respuesta aceptada
Guillaume
el 3 de Jun. de 2017
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 comentarios
Guillaume
el 5 de Jun. de 2017
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.
Más respuestas (1)
Phil
el 3 de Jun. de 2017
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 comentarios
Phil
el 3 de Jun. de 2017
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.
Ver también
Categorías
Más información sobre Multirate Signal Processing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!