Question

First of all I'm new to matlab and this forum so excuse my ignorance.

I generate a standard wiener process in the following way (I made it up myself, so if it's stupid or wrong I would like to know).

s =0.0001; % stepsize
t = [0:s:T]; % divide interval into steps

G=sqrt(s)*randn(length(t),1); 

Y=cumsum(G);

Now I want to find its values at some random times, say:

u=rand(4,1)

I figured out (with google and patience) to do something like

for i = 1:length(u)
   row(i) = find(t < u(i),1,'last');
end

and then simply take out the values from Y, but I would like to find a more direct way - do you have any suggestions?

Was it helpful?

Solution

What you're basically doing for each element in u is finding the index of the largest element in t. Try the following one-liner:

sum(bsxfun(@lt, repmat(t(:)', numel(u), 1), u(:)), 2)

What this does is:

  1. Generate a matrix using repmat, where each row equals t.
  2. Check each row using bsxfun for elements less than the corresponding element in u.
  3. Accumulate all the 1s in each row, essentially giving you the indices of the last smaller elements.

By the way, there is no need to put brackets ([]) in t = [0:s:T]. The colon operator (:) already outputs a vector.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top