Grupos de Google ya no admite nuevas publicaciones ni suscripciones de Usenet. El contenido anterior sigue siendo visible.

A simple question, looking for vectorized solution

Visto 0 veces
Saltar al primer mensaje no leído

Siyi

no leída,
25 feb 2009, 17:35:5925/2/09
a
Hey all,

Suppose I have a vector like: [ 3 2 4 1 3 2...]

I want to obtain a new vector like:
[3 3 3 2 2 4 4 4 4 1 3 3 3 2 2...]

basically every value repeates it's own number of times.


is there a simple vectorized way to do this?


thanks!

Bruno Luong

no leída,
25 feb 2009, 18:51:0125/2/09
a
One of the one-liner solutions:

v=[ 3 2 4 1 3 2 ]

cell2mat(arrayfun(@(x) x+zeros(1,x), v, 'uni', 0))

% Bruno

Matt Fig

no leída,
25 feb 2009, 18:52:0225/2/09
a
I don't know about simple, but here is a vectorized solution.

A = [ 3 2 4 1 3 2]
A(A==0) = []; % Need to get rid of zeros in the general case.
cs = cumsum(A);
B = zeros(1,cs(end));
B([1 cs(1:end-1)+1]) = 1;
B = A(cumsum(B))


SQ*eUd5Q_QoYRoU^_\f\}YI_`Qoeo=QX]][WvRS`__diQoXo_o^UdvoUQX0

Siyi

no leída,
25 feb 2009, 19:06:4525/2/09
a

Bruno, Matt Thanks you guys!

0 mensajes nuevos