Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A simple question, looking for vectorized solution

0 views
Skip to first unread message

Siyi

unread,
Feb 25, 2009, 5:35:59 PM2/25/09
to
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

unread,
Feb 25, 2009, 6:51:01 PM2/25/09
to
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

unread,
Feb 25, 2009, 6:52:02 PM2/25/09
to
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

unread,
Feb 25, 2009, 7:06:45 PM2/25/09
to

Bruno, Matt Thanks you guys!

0 new messages