0

i wanted to ask this: I have in mathematica :

step := {{0, 1}, {1, 0}, {0, -1}, {-1, 0}} [[RandomInteger[ {1, 4}] ]]

step  --> this takes one list from above (for example {0,1})

steps2D[n_] := Table[step, {n}]

and i did:

a=[0,1];b=[1,0];c=[0,-1];d=[-1,0];
list=[a;b;c;d]
step=@ (rand) rand(1,list) -->> i must extract from here randomly one pair..
step

steps2D=@ (n) arrayfun(step,n);

I have 2 problems: 1) I can't extract from my list randomly one pair. 2) I don't know if i have the step2D right.

EDIT-->> The code continues :

Walk2D[n_] := FoldList[Plus, {0, 0}, steps2D[n]]
Walk2D[10]
LastPoint2D[n_] := Fold[Plus, {0, 0}, steps2D[n]]
LastPoint2D[100]    

I did this :

Walk2D=@ (list,n) cumsum(steps2D(list,n));
Walk2D(list,10)

LastPoint2D = @ (Walk2D) (Walk2D(end));
walk1=Walk2D(list,100);
LastPoint2D(walk1)  -->> This gives me only one number and not a pair as it should
George
  • 5,808
  • 15
  • 83
  • 160
  • You will always have trouble trying to literally translate Mathematica into Matlab, since they have different philosophies. Mathematica encourages functional programming; Matlab instead operates on arrays. – nibot Feb 20 '11 at 23:41

1 Answers1

0

I suppose you want to do the first code example in Matlab. For getting a random element from the array list you could do something like:

rand_row_index = ceil( length(list) * rand(1) );
step = list( rand_row_index, : );

Concerning your second question, I'm not quite sure what you actually want to do, as I'm not familiar with Mathematica. If you want to constitute a matrix consisting of elements, randomly taken from list, you could write a function like

% Create a matrix with n rows that contains random values taken from list.
function result = steps2D(list, n)

    result = zeros(n, size(list, 2));

    for i = 1:n
        rand_row_index = ceil( length(list) * rand(1) );
        step = list( rand_row_index, : );
        result(i, :) = step;
    end

end

And use it like that:

steps = steps2D( list, 30 );

Note, that in Matlab, the function steps2D() must be defined in seperate file, convienently named steps2D.m.

Deve
  • 4,528
  • 2
  • 24
  • 27
  • @Deve:Hello,thanks for helping.I will look it tomorrow for the 1st solution to tell you if its ok.(i can't test it now).As for the 2nd i mean that it takes "step ,step.."n times,where step is one of the pairs in list. – George Feb 20 '11 at 22:22
  • @Deve: Hello,the code you gave me extracts only one number from the list.For example it extracts only "-1" instead of "-1 0".And if you could tell me how can i do it with function handle,as i write above?Thank you.. – George Feb 21 '11 at 10:36
  • You're right, I corrected the example. Hope, this is what you wanted to do. – Deve Feb 21 '11 at 12:21
  • @Deve:First of all ,thank you very much.That was exactly what i wanted it!I also created (with the same way) a function "step" because i have 2 (step & steps2D).Everything ok!Also,if you haid the time i'll be grateful if you could help me with this : http://stackoverflow.com/questions/5032029/map-command-and-few-other-from-mathematica-to-matlab..Again,thank you. – George Feb 21 '11 at 12:42
  • @Deve: Sorry ,but i am stack a little further in this code.I edited my original post ,if you could help me.. – George Feb 21 '11 at 13:04
  • First, in your new example, `Walk2D` returns a row vector as `cumsum()` adds the columns. So there's no necessity to explicitly read the last row of a vector that only contains one row. Second, if you want to get the last row of matrix `A`, the correct notation is `A(end,:)`. `A(end)` returns the very last element, i.e. a scalar. I'd rather not use so many function handles in the beginning. Try to use simple statements and you'll see what happens more easily. – Deve Feb 21 '11 at 13:26