Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Efficiently expanding a vector (with jitter...)
Replies: 2   Last Post: Jun 17, 2011 2:01 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Peter Mairhofer

Posts: 38
Registered: 8/18/10
Efficiently expanding a vector (with jitter...)
Posted: Jun 16, 2011 4:27 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.