Split an array according to a logic
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Is there a way to split a vector ,
say;
[1 2 3 6 7 8 12 13 14 15 ] ....( the actual vector is much longer )
into preassigned groups or cells such that cosecutive numbers
{1 2 3 } { 6 7 8} {12 13 14 15} .... are grouped in different cells.
Thanks a ton.
채택된 답변
If the numbers are ordered as you show them you can just use reshape() to make them into a m by 3 array. If you really need it to be a cell you can convert that using mat2cell
So for example
x = 1:30
X = reshape(x,10,3)
Xcell = mat2cell(X,ones(10,1))
댓글 수: 9
TV8
2021년 11월 1일
Its not always a sequence of 3 and I need a more general solution, where say a 20000 point vector , once sorted into ascending values can be grouped into smaller chunks of consecutive numbers.
Jon
2021년 11월 1일
I'm looking a little more closely at your example. I thought at first that that all of your groups had 3 elements. In which case you can use the approach I show above. Now I see that your last group has 4 elements. This would need a somewhat different solution. I'm not quite clear what your input and output are here. You mention consceutive numbers but in you example your input vector has 12,13,14,15 but then you make the group {11,13,14,15}. Is this just a typo.Can you describe further if it isn't simple groups of 3 e.g. {1,2,3}, {4,5,6},{7,8,9},...
TV8
2021년 11월 1일
Oh yeah that was a typo, sorry my bad, It needs to be a seperate group for each set of consective numbers.
TV8
2021년 11월 1일
Further explanation : It needs to be sorted into groups of consecutive numbers and it maynot be the same size so say further in the vector there there is 27 28 29 30 31 32 it need to be sorted into a unique group of 6 {27 28 29 30 31 32}. Then we have the groups : {1 2 3 } { 6 7 8} {12 13 14 15} and {27 28 29 30 31 32}
I think this will work for your situation
% example vector containing groups of consectutive numbers
x = [1 2 3 6 7 8 12 13 14 15 18 19 20 31];
% mark the consecutive elements, difference between consecutive elements is
% 1
idl = diff(x)== 1;
% mark where beginning of each group of consecutive elements occurs, these
% are jumps from 0 to 1
iStart = diff([0 idl])==1;
% count up jumps to form group numbers
groupCount = cumsum(iStart);
% assign group number, but only to ones that belong to a group (the
% consecutive values)
% (for non-members groupCount.*idl is zero so these are assigned to group
% zero)
group = groupCount.*idl ;
% loop through to assign to cell array by group
numGroups = max(group);
X = cell(numGroups,1);
for k = 1:numGroups
X{k} = x(k==group);
end
X =
4×1 cell array
{[ 1 2]}
{[ 6 7]}
{[12 13 14]}
{[ 18 19]}
Jon
2021년 11월 1일
OK glad that helped. I meant to add in my post that there might be some edge cases to consider at the very beginning and end of the vector, but hopefully you can work that out. If not I can have another look.
Jon
2021년 11월 1일
Oh, I see, I don't catch the last element in each group. I have to think about how to fix that.
I think this now catches the end of each consecutive group too. Most of the logic is the same, but I realized you have to look forward and backward to see if they are consecutive and not miss the ends of the groups
% example vector containing groups of consectutive numbers
x = [1 2 3 6 7 8 12 13 14 15 18 19 20 31 35 36 40 41];
% elements are consecutive if they the next element is one larger than they
% are
idlFwd = [diff(x)== 1 0];
% elements are also consecutive if the one before them is one less than they are
idlBwd = [0 flip(diff(flip(x))== -1)]
% elements are consecutive if either criteria is met
idl = idlFwd|idlBwd;
% mark where beginning of each group of consecutive elements occurs, these
% are jumps from 0 to 1
iStart = diff([0 idlFwd])==1;
% count up jumps to form group numbers
groupCount = cumsum(iStart);
% assign group number, but only to ones that belong to a group (the
% consecutive values)
% (for non-members groupCount.*idl is zero so these are assigned to group
% zero)
group = groupCount.*idl ;
% loop through to assign to cell array by group
numGroups = max(group);
X = cell(numGroups,1);
for k = 1:numGroups
X{k} = x(k==group);
end
disp(X)
1 2 3 6 7 8 12 13 14 15 18 19 20 31 35 36 40 41
{[ 1 2 3]}
{[ 6 7 8]}
{[12 13 14 15]}
{[ 18 19 20]}
{[ 35 36]}
{[ 40 41]}
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Historical Contests에 대해 자세히 알아보기
제품
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
