Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How can I repeat each element of a vector different times and store them in a new vector

Asked by Yufan Miao

Yufan Miao (view profile)

on 19 Feb 2013

Dear all,

I am a Matlab novice. I am sorry that my question may be silly.

Does anyone know that how I can repeat each element of a vector different times and then store them in a new vector?

Thank you in advance for the answers

Cheers!

0 Comments

Yufan Miao

Yufan Miao (view profile)

Tags

Products

No products are associated with this question.

3 Answers

Answer by Wayne King

Wayne King (view profile)

on 19 Feb 2013
Accepted answer

2 Comments

Oleg Komarov

Oleg Komarov (view profile)

on 19 Feb 2013

A Milestone.

Yufan Miao

Yufan Miao (view profile)

on 20 Feb 2013

Thank you very much for this quick reply. It really helps!

Wayne King

Wayne King (view profile)

Answer by Jos (10584)

Jos (10584) (view profile)

on 19 Feb 2013

Wayne pointed you to a run-length decoder/encoder.

Things become more simple if every element is to be repeated the same number of times:

% repeat all elements of A N times
A = [1 2 3]
N = 3 ; 
B1 = reshape(repmat(A(:).',N,1),1,[])
% or
nA = numel(A) ;
B2 = A(ceil((1:nA*N)/nA))

0 Comments

Jos (10584)

Jos (10584) (view profile)

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 20 Feb 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 21 Feb 2013
r = [1 3 3 4 0 5];
x = 1:6;
t = r > 0;
a = cumsum(r(t));
b = zeros(1,a(end));
b(a - r(t) + 1) = 1;
x1 = x(t);
out = x1(cumsum(b));

1 Comment

Andrei Bobrov

Andrei Bobrov (view profile)

on 21 Feb 2013

corrected

Andrei Bobrov

Andrei Bobrov (view profile)

Contact us