필터 지우기
필터 지우기

extract randomly a pair from a list

조회 수: 1 (최근 30일)
George
George 2011년 2월 21일
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.
  댓글 수: 1
George
George 2011년 2월 21일
Ok,thanks a lot!It works fine!

댓글을 달려면 로그인하십시오.

추가 답변 (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