2

I have a vector in Matlab A of dimension mx1 reporting the natural integers from 1 to m in increasing order, e.g. A=[1 2 3]'.

Let B be a vector of dimension mx1 reporting some natural integers greater or equal than zero, e.g. B=[1 3 0]'.

Without using loops, I want to construct a vector C of dimension sum_i(B(i)) obtained by listing each A(i) B(i) times. In the example C=[1 2 2 2].

TEX
  • 2,249
  • 20
  • 43
  • I think this is what you want: http://stackoverflow.com/questions/28501418/run-length-decoding-in-matlab – Daniel Jul 08 '15 at 15:57

1 Answers1

1

You can do it using arrayfun

A = [1 2 3]';
B = [1 3 0]';
m = 3;
C = cell2mat(arrayfun(@(x) ones(B(x),1)*A(x), 1:m, 'UniformOutput', 0)');

C =

     1
     2
     2
     2
Ikaros
  • 1,048
  • 11
  • 19