sequence grouping

조회 수: 1 (최근 30일)
Chien-Chia Huang
Chien-Chia Huang 2011년 4월 12일
Hi all,
I am trying to construct a few groups from a given sequence by giving some pivots, one or more. For example, a = 1:15 and the pivots are 6 and 9. The desired groups are [1:3], [4:8], [7:11] and [12:15]. Is loop (brute force) the only solution?
for i = 1:length(piv)
par{i} = piv(i)-length(piv):piv(i)+length(piv);
end
par1 = 1:piv(1)-1;
par2 = piv(2)+length(piv)+1:end;
Thanks.
  댓글 수: 3
Oleg Komarov
Oleg Komarov 2011년 4월 12일
I don't get the logic of the grouping...Can you elaborate a little more on the concept?
Chien-Chia Huang
Chien-Chia Huang 2011년 4월 13일
Sorry about being vague in description.
When a sequence [1:15] and pivots (6,9) are given, from each of the pivot, we enclose its neighbors (number of pivots from the right and left).
Hence, we have [(6-4):(6+2)] = [4:8] and [(9-2):(9+2)] = [7:11].
One of the rest will be those on the left of [4:8], namely, [1:3] and the other will be those on the right of [7:11], namely, [12:15].

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

채택된 답변

Matt Fig
Matt Fig 2011년 4월 13일
Does this do what you are wanting?
Lp = length(piv);
G = ones(Lp,2*Lp+1);
G(:,1) = piv-Lp;
G = cumsum(G,2);
D{1} = min(a):min(G(:))-1; % Holds the groups
D(2:Lp+1) = mat2cell(G,ones(1,Lp),2*Lp+1);
D{Lp+2} = max(G(:))+1:max(a);
.
EDIT
.
Note that this may be much faster, though it does have a loop.
L2 = length(piv);
D2 = cell(L2+2,1);
D2{1} = min(a):min(piv-L2-1);
for ii = 2:L2+1
D2{ii} = piv(ii-1)-L2:piv(ii-1)+L2;
end
D2{L2+2} = max(piv+L2)+1:max(a);
  댓글 수: 1
Chien-Chia Huang
Chien-Chia Huang 2011년 4월 13일
Thanks Matt.
Exactly what I want.

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 4월 12일
variant without loop
a = 1:15;
c = [6 9];
li=length(c);
lii = -li:li;
[non,I]=ismember(c,a);
C = bsxfun(@(x,y)x+y,I.',lii);
cl = cell(length(c)+2,1);
cl([1,end]) = {1:C(1)-1,C(end)+1:numel(a)};
cl([2:end-1]) = mat2cell(C,ones(length(c),1),l)
  댓글 수: 3
Matt Fig
Matt Fig 2011년 4월 13일
I bet the error message has to do with your use of an older version of MATLAB. Replace this line with:
[I,I] = ismember(c,a);
Chien-Chia Huang
Chien-Chia Huang 2011년 4월 13일
Thanks, Matt. That solves the error.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by