2

Consider A = [ 200000 x 1] vector. I have another vector idx = [200000x1]. I would like to tile A such that every ith element of A is tiled idx(i) times.

Eg:

A   idx output 
2   2    2
3   1    2
4   3    3 
5   1    4
.   .    4
.   .    4
.   .    5 
.    .     .

Any non-looping ideas?

Shai
  • 111,146
  • 38
  • 238
  • 371
enigmae
  • 349
  • 2
  • 10

1 Answers1

6

It seems like you are looking for run length decoding: that is idx(ii) represent the length A(ii) should be present in the encoded output.

Here's a nice way of doing that in Matlab:

output = zeros(1, sum(idx)); % allocate output
output( cumsum( [1 idx(1:end-1)] ) ) = 1;
output = A( cumsum( output ) );

output =

 2     2     3     4     4     4     5
Shai
  • 111,146
  • 38
  • 238
  • 371