필터 지우기
필터 지우기

How can I split a vector?

조회 수: 73 (최근 30일)
Stephanie Diaz
Stephanie Diaz 2017년 6월 2일
댓글: Stephanie Diaz 2017년 6월 5일
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.
Stephanie Diaz
Stephanie Diaz 2017년 6월 5일
@Phil thank you!

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

카테고리

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