|
|
Efficiently expanding a vector (with jitter...)
Posted:
Jun 16, 2011 4:27 PM
|
|
Hi,
Relatively simple task: I have a "low rate" vector, say:
[ 1 2 3 4 ]
Now this vector should be expanded by a factor R, say R = 3:
[ 1 1 1 2 2 2 3 3 3 4 4 4 ]
Currently I am doing
function y = expand(x, R) y = []; for elem=x.' y = [y ; elem*ones(R, 1)]; end
but this is not really really fast - especially for large data sets. However, the more problematic task is that I want to apply random, Gaussian "jitter", defined by sigma². Say, sigma²=0.1, the alternations should have the following offset:
[ 0 -1 0 1 ]
resulting in
[ 1 1 2 2 2 2 3 3 3 3 4 4 ]
Currently I am using:
function y = expand_rand(x, N, sigma) f_jitter = round(N*sigma*randn(length(x), 1)); y = []; rest = 0; i = 1; for elem=x.' amount = N + rest - f_jitter(i); y = [y ; elem*ones(amount, 1)]; rest = f_jitter(i); i = i + 1; end if length(y) < length(x)*N y = [y ; elem*ones(length(x)*N - length(y), 1)]; else y = y(1:(length(x)*N)); end
However, this is terribly slow! For a vector with 20k elements and R=1 this takes various minutes on a modern 4-core Xeon.
How to improve this?
Regards, Peter
|
|