sequence grouping
Mostrar comentarios más antiguos
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 comentarios
Matt Fig
el 12 de Abr. de 2011
That last line doesn't make sense. Are you missing a parenthesis around the END statement?
Oleg Komarov
el 12 de Abr. de 2011
I don't get the logic of the grouping...Can you elaborate a little more on the concept?
Chien-Chia Huang
el 13 de Abr. de 2011
Respuesta aceptada
Más respuestas (1)
Andrei Bobrov
el 12 de Abr. de 2011
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 comentarios
Chien-Chia Huang
el 13 de Abr. de 2011
Matt Fig
el 13 de Abr. de 2011
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
el 13 de Abr. de 2011
Categorías
Más información sobre Mathematics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!