Picking random points...

조회 수: 10 (최근 30일)
Joe Bannen
Joe Bannen 2015년 11월 5일
댓글: Thorsten 2015년 11월 5일
Hi
I am exploring a closed (time) interval between 0 and 1. I have used the linspace command to generate it:
t=linspace(0,1);
I can't seem to figure out how to randomly pick 10 (say) of these points in t so that I can plug them into a function (selected in a non-uniform way). Any thought?
Many thanks
Joe

채택된 답변

Stephen23
Stephen23 2015년 11월 5일
편집: Stephen23 2015년 11월 5일
You can simply generate some indices to pick value from the vector t. I have shown you uniformly distributed random indices, because you did not tell us what random distribution you want. Two solutions are shown below: without and with index repetition.
t = linspace(0,1);
N = 5; % how many points to pick
Without repetitions use randperm:
>> idx = randperm(numel(t),N)
idx =
45 75 58 51 15
>> t(idx)
ans =
0.44444 0.74747 0.57576 0.50505 0.14141
With repetitions use randi:
>> idy = randi(numel(t),1,N)
idy =
53 44 11 77 71
>> t(idy)
ans =
0.52525 0.43434 0.10101 0.76768 0.70707
  댓글 수: 4
Joe Bannen
Joe Bannen 2015년 11월 5일
Stephen
I think you were correct in the first answer, it is my lack of understanding that is causing the problem.
I had been using:
t_1=rand(10,1);
But now need to randomly pick 10 items from:
t=linspace(0,1);
Since randn uses the standard normal distribution, your suggestion should work! Please let me know if I am wrong on this.
Best wishes
Joe
Thorsten
Thorsten 2015년 11월 5일
Maybe you want to sort the 10 randomly picked numbers:
t=linspace(0,1);
t10 = t(sort(randperm(numel(t), 10)))

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by