Azzera filtri
Azzera filtri

extract randomly a pair from a list

1 visualizzazione (ultimi 30 giorni)
George
George il 21 Feb 2011
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.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by