extract randomly a pair from a list

1 次查看(过去 30 天)
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.

采纳的回答

David Young
David Young 2011-2-21
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by