Borrar filtros
Borrar filtros

extract randomly a pair from a list

1 visualización (últimos 30 días)
George
George el 21 de Feb. de 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.

Respuesta aceptada

David Young
David Young el 21 de Feb. de 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.
  1 comentario
George
George el 21 de Feb. de 2011
Ok,thanks a lot!It works fine!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by