MATLAB: Extract randomly a pair from a list

extractlistrandomly

Hello , i have this list a=[0,1];b=[1,0];c=[0,-1];d=[-1,0]; list=[a;b;c;d]
I want to extract randomly one of these four pairs and i want to it with a function handle.For example function= @ (i don't know what to put here(i think leaving it empty)) length(list)*rand… Anyway,sth like that. If i call the function it will extract one pair from my list.For example, "0 1".
Thank you.

Best Answer

Two suggestions. First:
selector1 = @() list(ceil(rand*size(list,1)), :);
Then each time you call
x = selector1()
you will get a random row of list. Note that the list is fixed once selector1 has been constructed: selector1 will always give you rows from the original list, even if you assign a new matrix to the variable "list".
So the second suggestions is:
selector2 = @(list) list(ceil(rand*size(list,1)), :);
which accepts an argument. You get random rows with
x = selector2(list);
This means that you can now get a random row of any matrix, not just the one hard-wired into the selector, and the current value of "list" will be used.
Which one of these you need depends on how you're going to use the function handle.